标签:des style blog http color io os ar java
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 621 Accepted Submission(s): 161
#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 0x3f3f3f3f using 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 ; }
标签:des style blog http color io os ar java
原文地址:http://www.cnblogs.com/20120125llcai/p/4027498.html