Difference
Time Limit: 2000/1000 MS (Java/others) memory limit: 65535/65535 K (Java/Others)
Total submission (s): 621 accepted submission (s): 161
Problem descriptiona 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.
Inputthe 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.
Outputfor each test case, output a string in one line. Output "yes" if the graph is a difference, and "no" if it is not a difference.
Sample input3 4 0011 0001 1000 1100 4 0111 1001 1001 1110 3 000000 000
Sample outputyes No Yes
HintIn 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. source2013 ACM-ICPC Jilin Tonghua national invitational competition-Questions reproduce to give you a picture, ask is not meet the requirements of the question, | A [I]-A [J] |> = T; for the edges in the vertex that are not in the vertex | A [I]-A [J] | <t | A [I] | <t; it is easy to know that if I-j has edges, a [I], a [J] symbols must be different, then this is a bipartite graph, the staining method can be used. If the dyeing method can be used, we can obtain a T value. Then construct the difference constraint equation and run the shortest short circuit to determine the negative circle.
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<queue>#include<vector>#include<set>#include<stack>#include<map>#include<ctime>#include<bitset>#define LL long long#define mod 1000000007#define maxn 410#define pi acos(-1.0)#define eps 1e-8#define INF 0x3f3f3f3fusing namespace std;vector<int>qe[maxn] ;int color[maxn] ,flag,top,val[maxn*maxn] ;int head[maxn],to[maxn*maxn],next1[maxn*maxn];char a[maxn][maxn] ;void Unit(int u,int v,int c){ next1[top] = head[u] ;to[top] = v ; val[top]=c;head[u]=top++;}void dfs(int u,int fa,int c ){ color[u]=c; for(int i = 0 ; i < qe[u].size();i++) { int v = qe[u][i] ; if(v==fa) continue ; if(color[v]==-1) { dfs(v,u,1-c) ; } else if(color[v]==c) { flag=1; return ; } }}int dis[maxn] ,cnt[maxn];bool vi[maxn];bool spfa(int s,int n){ queue<int>q; for(int i = 1 ; i <= n ;i++) { dis[i]=0; vi[i]=1; q.push(i); cnt[i]=0; } int i,u,v; while(!q.empty()) { u = q.front();q.pop(); for( i = head[u] ; i != -1 ; i = next1[i]) { v = to[i] ; if(dis[v] > dis[u]+val[i]) { dis[v]=dis[u]+val[i] ; if(!vi[v]) { vi[v]=true; cnt[v]++; if(cnt[v]>n) return false; q.push(v) ; } } } vi[u]=false; } return true;}int main(){ int j,i,l,g,n; int T,ans1,u,v; cin >> T ; while(T--) { scanf("%d",&n) ; for( i = 1 ; i <= n ;i++){ scanf("%s",a[i]+1) ; qe[i].clear(); } for( i = 1 ; i <= n ;i++) for( j = 1 ; j <= n ;j++)if(a[i][j]==‘1‘){ qe[i].push_back(j); } memset(color,-1,sizeof(color)) ; flag=0; for( i = 1 ; i <= n ;i++)if(color[i]==-1){ dfs(i,-1,1) ; if(flag) break ; } if(flag){ puts("No") ; continue ; } top=0; memset(head,-1,sizeof(head)) ; for( i = 1 ; i <= n ;i++) for( j = i+1 ; j <= n ;j++) { if(a[i][j]==‘1‘) { if(color[i]) Unit(i,j,-maxn) ; else Unit(j,i,-maxn) ; } else { if(color[i]) Unit(j,i,maxn-1); else Unit(i,j,maxn-1) ; } } if(spfa(1,n))puts("Yes") ; else puts("No") ; } return 0 ;}View code
HDU 4598 difference