Advertisement
Insyder01

Untitled

Mar 24th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.87 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define L(i, m, n) for(int i(m);i < n;i++)
  3. #define pb push_back
  4. #define D(X) cout<<"  "<<#X": "<<X<<endl;
  5. #define in(x) cin >> x
  6. #define SZ(X) int(X.size())
  7. #define clr(A, V) L(i, 0, sizeof(A)) A[i]=V
  8. #define ff first
  9. #define RF(X) freopen(X, "r", stdin)
  10. #define WF(X) freopen(X, "w", stdout)
  11. using namespace std;
  12. typedef long long ll;
  13. typedef pair<ll,ll> pll;
  14. typedef vector<int> vi;
  15. typedef vector<ll> vll;
  16. typedef pair<int,int> pii;
  17. typedef vector<pii> vpii;
  18. typedef pair<int, string> pis;
  19. typedef vector<string> vs;
  20. typedef pair<pair<int, int>, pair<int, int > > piiii;
  21. typedef vector<vi> vii;
  22. const int maxn=202;
  23. bool vis[maxn];
  24. int F(int s,vii &adj,vi &color){
  25.     queue<int>q;q.push(s);
  26.     color[s]=0;
  27.     bool IS=1;
  28.     while(!q.empty()&&IS){
  29.         int u=q.front();q.pop();
  30.         L(j,0,SZ(adj[j])){
  31.             int c=adj[u][s];
  32.             if(color[c]==1e9){
  33.                 color[c]=1-color[u];
  34.                 q.push(c);
  35.             }
  36.             else if(color[u]==color[c]){IS=0;break;}
  37.         }
  38.     }
  39.     if(!IS)return -1;
  40.     int w=0,b=0;
  41.     L(i,0,maxn)if(!color[i])b++;else if(color[i]==1)w++;
  42.     return min(w,b);
  43. }
  44. int main(){
  45.     int t,n,e,x,y;in(t);
  46.     while(t--){
  47.         int ans=0;
  48.         in(n),in(e);
  49.         vector<vector<int> > adj(maxn,vector<int>());
  50.         for(int i = 0;i < e;i++){
  51.             D(i);
  52.             cin >> x >> y;
  53.             adj[x].pb(y);
  54.             adj[y].pb(x);
  55.         }
  56.         vi color(maxn,1e9);bool is=1;
  57.         L(i,0,n){
  58.             if(color[i]==1e9){
  59.                 int ret=F(i,adj,color);
  60.                 if(ret==-1){
  61.                     printf("-1\n");
  62.                     is=0;
  63.                     break;
  64.  
  65.                 }
  66.                 else ans+=ret;
  67.             }
  68.  
  69.  
  70.         }
  71.         if(is)printf("%d\n",ans);
  72.     }
  73.  
  74.  
  75.  
  76.  
  77.     return 0;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement