iscsi

cc 12 ambidex 2

Jan 9th, 2012
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.61 KB | None | 0 0
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <sstream>
  4. #include <string>
  5. #include <vector>
  6. #include <list>
  7. #include <queue>
  8. #include <stack>
  9. #include <set>
  10. #include <map>
  11. #include <cstdio>
  12. #include <cstdlib>
  13. #include <cctype>
  14. #include <cmath>
  15. #include <cstring>
  16. #include <ctime>
  17. #include <cassert>
  18. using namespace std;
  19. typedef unsigned long long LL;
  20. #define FOR(k,a,b) for(int k(a); k < (b); ++k)
  21. #define FORD(k,a,b) for(int k(a-1); k >= (b); --k)
  22. #define REP(k,a) for(int k=0; k < (a); ++k)
  23. #define REPD(k,a) for(int k=a-1; k >-1; --k)
  24. #define ABS(a) ((a)>0?(a):-(a))
  25. #define INF 1e9
  26.  
  27. inline int score(int a, int b)
  28. {
  29.     return min(a,b);
  30. }
  31.  
  32. inline int finalScore(int round,int N, const vector<int>& used)
  33. {
  34.     int H=0;
  35.     REP(i,used.size())
  36.         if(used[i])
  37.             ++H;
  38.     return round*N-H;
  39. }
  40.  
  41. int main()
  42. {
  43. #ifdef HOME
  44.     freopen ("in.txt","r",stdin);
  45.     freopen ("out.txt","w",stdout);
  46. #endif
  47.     int T,N,M;
  48.     vector<pair<int,int> > data;
  49.     vector<int> histo, bestused, used, actual;
  50.     vector<vector<int> > used2;
  51.     data.reserve(1001);
  52.     histo.reserve(101);
  53.     used.reserve(1001);
  54.     used2.reserve(101);
  55.     scanf("%d",&T);
  56.     REP(testcase,T)
  57.     {
  58.         scanf("%d %d",&N,&M);
  59.         data.clear();
  60.         data.resize(M);
  61.         histo.clear();
  62.         histo.resize(N);
  63.         used2.clear();
  64.         REP(i,M)
  65.             scanf("%d",&(data[i].first));
  66.         REP(i,M)
  67.             scanf("%d",&(data[i].second));
  68.         REP(i,M)
  69.         {
  70.             ++histo[--data[i].first];
  71.             ++histo[--data[i].second];
  72.         }
  73.         bool found=true;
  74.         used.clear();
  75.         used.resize(M,0);
  76.         bestused.clear();
  77.         int bestscore=0;
  78.         int round=0;
  79.         do
  80.         {
  81.             ++round;
  82.             actual.clear();
  83.             used2.resize(used2.size()+1);
  84.             vector<int>& actused2=used2.back();
  85.             actused2.resize(N,0);
  86.             int best,bestval;
  87.             do
  88.             {
  89.                 best=-1,bestval=INF;
  90.                 REP(i,M)
  91.                 {
  92.                     if(!used[i] && !actused2[data[i].first] && !actused2[data[i].second] && bestval>score(histo[data[i].first],histo[data[i].second]))
  93.                     {
  94.                         best=i;
  95.                         bestval=score(histo[data[i].first],histo[data[i].second]);
  96.                     }
  97.                 }
  98.                 if(best>-1)
  99.                 {
  100.                     used[best]=round;
  101.                     ++actused2[data[best].first];
  102.                     ++actused2[data[best].second];
  103.                     --histo[data[best].first];
  104.                     --histo[data[best].second];
  105.                     actual.push_back(best);
  106.                 }
  107.             }while(best!=-1);
  108.             do
  109.             {
  110.                 best=-1,bestval=0;
  111.                 REP(i,M)
  112.                 {
  113.                     if(!used[i] && (!actused2[data[i].first] || !actused2[data[i].second] )&& bestval<score(histo[data[i].first],histo[data[i].second]))
  114.                     {
  115.                         best=i;
  116.                         bestval=score(histo[data[i].first],histo[data[i].second]);
  117.                     }
  118.                 }
  119.                 if(best!=-1)
  120.                 {
  121.                     used[best]=round;
  122.                     ++actused2[data[best].first];
  123.                     ++actused2[data[best].second];
  124.                     --histo[data[best].first];
  125.                     --histo[data[best].second];
  126.                     actual.push_back(best);
  127.                 }
  128.             }while(best!=-1);
  129.             //clear unnecessary
  130.             do
  131.             {
  132.                 best=-1,bestval=INF;
  133.                 REP(i,actual.size())
  134.                 {
  135.                     int& actref=actual[i];
  136.                     if(actref>-1 && actused2[data[actref].first]>1 && actused2[data[actref].second]>1 && bestval>score(histo[data[actref].first],histo[data[actref].second]))
  137.                     {
  138.                         best=i;
  139.                         bestval=score(histo[data[actref].first],histo[data[actref].second]);
  140.                     }
  141.                 }
  142.                 if(best!=-1)
  143.                 {
  144.                     int& actref=actual[best];
  145.                     used[actref]=0;
  146.                     ++histo[data[actref].first];
  147.                     ++histo[data[actref].second];
  148.                     --actused2[data[actref].first];
  149.                     --actused2[data[actref].second];
  150.                     actref=-1;
  151.                 }
  152.             }while(best!=-1);
  153.             if(histo.end()==find(histo.begin(),histo.end(),0))
  154.                 continue;
  155.             else
  156.             {
  157. //              bool changed=true;
  158. //              //while(changed)
  159. //              {
  160. //                  changed=false;
  161.                 FOR(rr,1,round+1)
  162.                 {
  163.                     vector<int> actualroundedge,remainededge;
  164.                     REP(j,M)
  165.                     {
  166.                         if(used[j]==0)
  167.                         {
  168.                             remainededge.push_back(j);
  169.                         }
  170.                         else if(used[j]==rr)
  171.                         {
  172.                             actualroundedge.push_back(j);
  173.                         }
  174.                     }
  175.                     vector<int>& w=used2[rr-1];
  176.                     REP(i,w.size())
  177.                     if(w[i]>1)
  178.                     {
  179.                         vector<int> A(N,-1),AA(N,-1),B(N,-1),BB(N,-1);
  180.                         int found=-1;
  181.                         deque<pair<int,int> > q;
  182.                         q.push_back(make_pair(i,0));
  183.                         while(q.size())
  184.                         {
  185.                             const pair<int,int> act=q.front();
  186.                             const int& actnode=act.first;
  187.                             q.pop_front();
  188.                             if(act.second%2)
  189.                             {
  190.                                 REP(j,remainededge.size())
  191.                                     if(data[remainededge[j]].first==actnode || data[remainededge[j]].second==actnode)
  192.                                     {
  193.                                         int othernode=data[remainededge[j]].first==actnode?data[remainededge[j]].second:data[remainededge[j]].first;
  194.                                         if(B[othernode]==-1)
  195.                                         {
  196.                                             B[othernode]=remainededge[j];
  197.                                             BB[othernode]=actnode;
  198.                                             q.push_back(make_pair(othernode,act.second+1));
  199.                                         }
  200.                                     }
  201.                             }
  202.                             else
  203.                             {
  204.                                 REP(j,actualroundedge.size())
  205.                                     if(data[actualroundedge[j]].first==actnode || data[actualroundedge[j]].second==actnode)
  206.                                     {
  207.                                         int othernode=data[actualroundedge[j]].first==actnode?data[actualroundedge[j]].second:data[actualroundedge[j]].first;
  208.                                         if(A[othernode]==-1)
  209.                                         {
  210.                                             A[othernode]=actualroundedge[j];
  211.                                             AA[othernode]=actnode;
  212.                                             q.push_back(make_pair(othernode,act.second+1));
  213.                                             if((othernode==i && w[i]>2) || ( w[i]>1 && w[othernode]>1))
  214.                                             {
  215.                                                 found=othernode;
  216.                                                 q.clear();
  217.                                                 break;
  218.                                             }
  219.                                         }
  220.                                     }
  221.                             }
  222.                         }
  223.                             if(found>-1)
  224.                             {
  225. //                              changed=true;
  226.                                 vector<int> newedge,oldedge;
  227.                                 oldedge.push_back(A[found]);
  228.                                 found=AA[found];
  229.                                 for(int kk=0;found!=i;++kk)
  230.                                 {
  231.                                     if(kk%2)
  232.                                     {
  233.                                         oldedge.push_back(A[found]);
  234.                                         found=AA[found];
  235.                                     }
  236.                                     else
  237.                                     {
  238.                                         newedge.push_back(B[found]);
  239.                                         found=BB[found];
  240.                                     }
  241.                                 }
  242.                                 REP(kk,newedge.size())
  243.                                 {
  244.                                     int& actref=newedge[kk];
  245.                                     used[actref]=rr;
  246.                                     --histo[data[actref].first];
  247.                                     --histo[data[actref].second];
  248.                                     ++actused2[data[actref].first];
  249.                                     ++actused2[data[actref].second];
  250.                                 }
  251.                                 REP(kk,oldedge.size())
  252.                                 {
  253.                                     int& actref=oldedge[kk];
  254.                                     used[actref]=0;
  255.                                     ++histo[data[actref].first];
  256.                                     ++histo[data[actref].second];
  257.                                     --actused2[data[actref].first];
  258.                                     --actused2[data[actref].second];
  259.                                 }
  260.                                 break;
  261.                             }
  262.                         }
  263.                 }
  264.                 break;
  265.             }
  266.                 //if(!changed)
  267. //          }
  268.         }while(true);
  269.         //while(histo.end()==find(histo.begin(),histo.end(),0));
  270.         REP(i,M)
  271.             printf("%d ",/*best*/used[i]);
  272.         printf("\n");
  273.     }
  274.     return 0;
  275. }
Advertisement
Add Comment
Please, Sign In to add comment