Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2097 Accepted Submission(s): 756
1 #include<bits/stdc++.h> 2 //#include<regex> 3 #define db double 4 #include<vector> 5 #define ll long long 6 #define vec vector<ll> 7 #define Mt vector<vec> 8 #define ci(x) scanf("%d",&x) 9 #define cd(x) scanf("%lf",&x) 10 #define cl(x) scanf("%lld",&x) 11 #define pi(x) printf("%d\n",x) 12 #define pd(x) printf("%f\n",x) 13 #define pl(x) printf("%lld\n",x) 14 #define MP make_pair 15 #define PB push_back 16 #define inf 0x3f3f3f3f3f3f3f3f 17 #define fr(i,a,b) for(int i=a;i<=b;i++) 18 const int N=1e3+5; 19 const int mod=1e9+7; 20 const int MOD=mod-1; 21 const db eps=1e-18; 22 const db PI=acos(-1.0); 23 using namespace std; 24 vector<int> g[N]; 25 int n,m,x,y,ok; 26 int vis[N]; 27 void dfs(int u) 28 { 29 int f=0; 30 if(vis[u]==-1) vis[u]=0,f=1; 31 for(int i=0;i<g[u].size();i++){ 32 int v=g[u][i]; 33 if(vis[v]==-1) vis[v]=1-vis[u],dfs(v);//染色 34 else if(vis[v]==vis[u]&&f==1) f=0,vis[u]=1-vis[v];//最多变换一次染色方式 35 else if(vis[v]==vis[u]) ok=0;//矛盾 36 } 37 } 38 int main() 39 { 40 while(scanf("%d%d%d%d",&n,&m,&x,&y)==4) 41 { 42 ok=1; 43 for(int i=1;i<=n;i++) g[i].clear(); 44 memset(vis,-1, sizeof(vis)); 45 while(m--) 46 { 47 int u,v; 48 ci(u),ci(v); 49 g[u].push_back(v); 50 g[v].push_back(u); 51 } 52 while(x--){ 53 int u;ci(u);vis[u]=0; 54 } 55 while(y--){ 56 int u;ci(u);vis[u]=1; 57 } 58 for(int i=1;i<=n;i++){ 59 if(g[i].size()) dfs(i); 60 } 61 for(int i=1;i<=n;i++) if(vis[i]==-1) ok=0;//不联通 62 if(ok) puts("YES"); 63 else puts("NO"); 64 } 65 return 0; 66 }
原文:http://www.cnblogs.com/mj-liylho/p/7653513.html