2015
09-17

# Difference

A graph is a difference if every vertex vi can be assigned a real number ai and there exists a positive real number T such that
(a) |ai| < T for all i and
(b) (vi, vj) in E <=> |ai – aj| >= T,
where E is the set of the edges.
Now given a graph, please recognize it whether it is a difference.

The first line of input contains one integer TC(1<=TC<=25), the number of test cases.
Then TC test cases follow. For each test case, the first line contains one integer N(1<=N<=300), the number of vertexes in the graph. Then N lines follow, each of the N line contains a string of length N. The j-th character in the i-th line is "1" if (vi, vj) in E, and it is "0" otherwise. The i-th character in the i-th line will be always "0". It is guaranteed that the j-th character in the i-th line will be the same as the i-th character in the j-th line.

The first line of input contains one integer TC(1<=TC<=25), the number of test cases.
Then TC test cases follow. For each test case, the first line contains one integer N(1<=N<=300), the number of vertexes in the graph. Then N lines follow, each of the N line contains a string of length N. The j-th character in the i-th line is "1" if (vi, vj) in E, and it is "0" otherwise. The i-th character in the i-th line will be always "0". It is guaranteed that the j-th character in the i-th line will be the same as the i-th character in the j-th line.

3
4
0011
0001
1000
1100
4
0111
1001
1001
1110
3
000
000
000

Yes
No
Yes
Hint
In sample 1, it can let T=3 and a[sub]1[/sub]=-2, a[sub]2[/sub]=-1, a[sub]3[/sub]=1, a[sub]4[/sub]=2.


uv相连时，设u是正数,很明显有u-v<=-T

uv不连时,设u是正数,有v-u<=T-1。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
using namespace std;

char mp[305][305];
int col[305];
const int T=400;

struct Edge{
int u,v,w;
int next;
}edge[300*300*2];
int dis[305],cnt[305],n; bool vis[305];

bool Dcol(int u){
for(int j=0;j<n;j++){
if(mp[u][j]=='1'){
if(col[j]==0){
col[j]=col[u]*(-1);
if(!Dcol(j)){ return false;
}
}
else if(col[j]==col[u]) return false;
}
}
return true;
}

edge[tot].u=u;
edge[tot].v=v;
edge[tot].w=w;
}

bool spfa(){
queue<int>q;
for(int i=0;i<=n;i++){
dis[i]=1<<30;
}
dis[0]=0;
memset(vis,false,sizeof(vis));
memset(cnt,0,sizeof(cnt));
q.push(0);
vis[0]=true;
while(!q.empty()){
int u=q.front(); q.pop();
vis[u]=false;
int v=edge[e].v;
if(dis[u]+edge[e].w<dis[v]){
dis[v]=dis[u]+edge[e].w;
cnt[v]++;
if(cnt[v]>n) return false;
if(!vis[v]){
q.push(v);
vis[v]=true;
}
}
}
}
return true;
}

int main(){
int Tc;
scanf("%d",&Tc);
while(Tc--){
scanf("%d",&n);
memset(col,0,sizeof(col));
for(int i=0;i<n;i++){
scanf("%s",mp[i]);
}
bool flag=true;
for(int i=0;i<n;i++){
if(col[i]==0){
col[i]=1;
if(!Dcol(i)){
flag=false;
break;
}
}
}
if(!flag){
puts("No");
}
else{
tot=0;
int u,v;
for(int i=0;i<n;i++){
u=i+1;
if(col[i]>0){
}
else{
}
for(int j=i+1;j<n;j++){
v=j+1;
if(col[i]<0) swap(u,v);
if(mp[i][j]=='1'){
}
else{
if(col[i]==col[j]) continue;
}
}
}
if(spfa()){
puts("Yes");
}
else puts("No");
}
}
return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#define Maxn 1010
#define Maxm Maxn*Maxn
#define inf 100000000
#define T 400
using namespace std;
void init()
{
memset(vi,0,sizeof(vi));
memset(map,0,sizeof(map));
memset(col,-1,sizeof(col));
e=0;
}
struct Edge{
int u,next,v,val;
}edge[Maxm];
{
}
{
}
void find(int u,int c)
{
int i,j,temp;
vi[u]=1;
temp=edge[i].v;
if(col[temp]==-1){
col[temp]=c;
find(temp,c^1);
}
}
}
int spfa()
{
int i,j,v,u;
queue<int> q;
memset(cnt,0,sizeof(cnt));
memset(vi,0,sizeof(vi));
for(i=0;i<=n;i++){
dis[i]=inf;
}
dis[0]=0;
q.push(0);
while(!q.empty()){
int u=q.front();
q.pop();
vi[u]=0;
v=edge[i].v;
if(dis[v]>dis[u]+edge[i].val){
dis[v]=dis[u]+edge[i].val;
cnt[v]++;
if(cnt[v]>n) return 0;
if(!vi[v]){
q.push(v);
vi[v]=1;
}
}
}
}
return 1;
}
int solve()
{
int i,j,u,v;
for(i=1;i<=n;i++)  if(!vi[i]) find(i,0);
for(i=0;i<e;i++) if(col[edge[i].u]==col[edge[i].v]) return 0;
e=0;
for(i=1;i<=n;i++){
for(j=i+1;j<=n;j++){
if(!map[i][j]&&col[i]==col[j]) continue;
u=i,v=j;
if(col[u]==0)
swap(u,v);
if(map[u][v])
else
}
if(col[i]==0){
}
else{
}
}
return spfa();
}
int main()
{
int t,i,j;
char str[310];
scanf("%d",&t);
while(t--){
scanf("%d",&n);
init();
for(i=1;i<=n;i++){
scanf("%s",&str);
for(j=0;j<n;j++){
if(str[j]=='1'&&!map[i][j+1]){
map[i][j+1]=map[j+1][i]=1;
}
}
}
if(!solve())
printf("No\n");
else
printf("Yes\n");
}
return 0;
}