luogu_1330 封鎖陽光大學
阿新 • • 發佈:2017-09-19
dfs () color 大學 main == clu logs urn
#include <cstdio> #include <vector> #include <iostream> using namespace std; const int maxn=100010; int n,m,sum,sum1,sum2; vector<int> G[maxn]; int color[maxn],color2[maxn]; bool dfs(int u){ for(int i=0;i<G[u].size();i++){ int v=G[u][i]; if(!color[v]){ color[v]=3-color[u]; if(color[v]==2)sum2++; if(color[v]==1)sum1++; if(!dfs(v))return false; } else if(color[u]==color[v])return false; } return true; } bool solve(){ for(int i=1;i<=n;i++) if(!color[i]){ sum1=sum2=0; color[i]=1; sum1++; if(!dfs(i))return false; else sum+=min(sum1,sum2); } return true; } int main(){ scanf("%d%d",&n,&m); while(m--){ int u,v; scanf("%d%d",&u,&v); G[u].push_back(v); G[v].push_back(u); } if(solve())printf("%d\n",sum); else puts("Impossible"); return 0; }
luogu_1330 封鎖陽光大學