Advertisement
Guest User

ghizi

a guest
Oct 22nd, 2010
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.92 KB | None | 0 0
  1. #include<fstream>
  2. #include<vector>
  3. #include<queue>
  4. #include<string.h>
  5. #define inf "ghizi.in"
  6. #define outf "ghizi.out"
  7. #define NMax 5100
  8. #define VMax 103
  9. #define INF 0x3f3f3f3f
  10. using namespace std;
  11.  
  12. fstream f(inf,ios::in),g(outf,ios::out);
  13.  
  14. int N,K;
  15. int viz[VMax];
  16. int C[VMax][VMax],F[VMax][VMax];
  17. int T[VMax];
  18.  
  19. const int s = 102;
  20. const int d = 101;
  21.  
  22. vector<int> LA[VMax];
  23.  
  24. struct arc { int x,y,uz; } G[NMax];
  25.  
  26. void read()
  27. {
  28.     f>>N>>K;;
  29.     int t1,t2;
  30.     for(int i=1; i<=N; i++)
  31.     {
  32.         f>>t1>>t2;
  33.         C[t1+1][t2+1]++;       
  34.         LA[t1+1].push_back(t2+1); LA[t2+1].push_back(t1+1);    
  35.         G[i].x = t1+1; G[i].y = t2+1;
  36.     }
  37.     C[s][1] = K;
  38.     LA[s].push_back(1); LA[1].push_back(s);
  39. }
  40.  
  41. int bfs()
  42. {
  43.     queue<int> q;
  44.     memset( viz, 0, sizeof(viz) );
  45.     viz[s] = 1;
  46.     q.push(s);
  47.     while( !q.empty() )
  48.     {
  49.         int n = q.front(); q.pop();
  50.         if( n==d ) continue;
  51.         for(unsigned i=0; i<LA[n].size(); i++)
  52.         {
  53.             int x = LA[n][i];
  54.             if( viz[x] || F[n][x] == C[n][x] ) continue;
  55.             q.push(x);
  56.             viz[x] = 1;
  57.             T[x] = n;
  58.         }
  59.     }
  60.     return viz[d];
  61. }
  62.  
  63. inline int min(int a,int b) { return ( a<b ? a : b ); }
  64.  
  65. void flux()
  66. {
  67.     while( bfs() )
  68.     {
  69.         for(unsigned i=0; i<LA[d].size(); i++)
  70.         {
  71.             int v = LA[d][i];
  72.             if( !viz[v] || F[v][d]==C[v][d] ) continue;
  73.             T[d] = v;
  74.            
  75.             int fmin = INF;
  76.            
  77.             for( v = d; v!=s; v = T[v] )
  78.                 fmin = min( fmin, C[ T[v] ][v] - F[ T[v] ][v] );
  79.            
  80.             if( fmin==0 ) continue;
  81.            
  82.             for( v = d; v != s; v = T[v] )
  83.             {
  84.                 F[ T[v] ][v] += fmin;
  85.                 F[v][ T[v] ] -=fmin;
  86.             }
  87.         }
  88.     }
  89. }
  90.  
  91. void print()
  92. {
  93.     int nr=0;
  94.     for(int i=1; i<VMax-1; i++)
  95.         for(int j=1; j<VMax-1; j++)
  96.             if( F[i][j] )
  97.                 for(int p=1; p<=N; p++)
  98.                     if( G[p].x==i && G[p].y==j && G[p].uz==0 ) { G[p].uz=1; nr++; break; }
  99.     g<<nr<<"\n";
  100.     for(int i=1; i<=N; i++)
  101.         if( G[i].uz ) g<<i<<" ";
  102. }
  103.  
  104. void solve()
  105. {
  106.     flux();
  107.     print();
  108. }
  109.  
  110. int main()
  111. {
  112.     read(); solve();
  113.     f.close(); g.close();
  114.     return 0;
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement