Exchanging Gifts--2019CCPC哈尔滨 E题

题意:http://codeforces.com/gym/102394/problem/E

1操作是给你一串数,2操作是连结两个串(所以可能很长),问你最后一个串的值(知道最多的个数就很好算,关键计算个数)

思路:

对二操作建图,一开始还以为建出来的是树就可以直接BFS计算次数,自闭了好久,最后才发现是拓扑图,一点点扒点才行。

离散化加统计个数啥的导致代码冗长,奇奇怪怪的数组贼多。

  1 #define IOS ios_base::sync_with_stdio(0); cin.tie(0);
  2 #include <cstdio>//sprintf islower isupper
  3 #include <cstdlib>//malloc  exit strcat itoa system("cls")
  4 #include <iostream>//pair
  5 #include <fstream>//freopen("C:\\Users\\13606\\Desktop\\Input.txt","r",stdin);
  6 #include <bitset>
  7 //#include <map>
  8 #include<unordered_map>
  9 #include <vector>
 10 #include <stack>
 11 #include <set>
 12 #include <string.h>//strstr substr
 13 #include <string>
 14 #include <time.h>// srand(((unsigned)time(NULL))); Seed n=rand()%10 - 0~9;
 15 #include <cmath>
 16 #include <deque>
 17 #include <queue>//priority_queue<int, vector<int>, greater<int> > q;//less
 18 #include <vector>//emplace_back
 19 //#include <math.h>
 20 #include <cassert>
 21 //#include <windows.h>//reverse(a,a+len);// ~ ! ~ ! floor
 22 #include <algorithm>//sort + unique : sz=unique(b+1,b+n+1)-(b+1);+nth_element(first, nth, last, compare)
 23 using namespace std;//next_permutation(a+1,a+1+n);//prev_permutation
 24 //******************
 25 int abss(int a);
 26 int lowbit(int n);
 27 int Del_bit_1(int n);
 28 int maxx(int a,int b);
 29 int minn(int a,int b);
 30 double fabss(double a);
 31 void swapp(int &a,int &b);
 32 clock_t __STRAT,__END;
 33 double __TOTALTIME;
 34 void _MS(){__STRAT=clock();}
 35 void _ME(){__END=clock();__TOTALTIME=(double)(__END-__STRAT)/CLOCKS_PER_SEC;cout<<"Time: "<<__TOTALTIME<<" s"<<endl;}
 36 //***********************
 37 #define rint register int
 38 #define fo(a,b,c) for(rint a=b;a<=c;++a)
 39 #define fr(a,b,c) for(rint a=b;a>=c;--a)
 40 #define mem(a,b) memset(a,b,sizeof(a))
 41 #define pr printf
 42 #define sc scanf
 43 #define ls rt<<1
 44 #define rs rt<<1|1
 45 typedef pair<int,int> PII;
 46 typedef vector<int> VI;
 47 typedef long long ll;
 48 const double E=2.718281828;
 49 const double PI=acos(-1.0);
 50 const ll INF=(1LL<<60);
 51 const int inf=(1<<30);
 52 const double ESP=1e-9;
 53 const int mod=(int)1e9+7;
 54 const int N=(int)1e6+10;
 55 
 56 int a[N],b[N],in[N];
 57 ll much[N];
 58 ll mark[N];
 59 void LS(int n)
 60 {
 61     int m=0;
 62     for(int i=1;i<=n;++i)
 63         b[++m]=a[i];
 64     sort(b+1,b+1+m);
 65     m=unique(b+1,b+1+m)-b-1;
 66     for(int i=1;i<=n;++i)
 67         a[i]=lower_bound(b+1,b+1+m,a[i])-b;
 68     return;
 69 }
 70 vector<vector<int> >arr(N);
 71 struct node
 72 {
 73     int to,next;
 74     ll val;
 75 }edge[N*10];
 76 int head[N];
 77 ll val[N];
 78 bool is[N];
 79 int tot;
 80 void Init(int n)
 81 {
 82     tot=0;
 83     for(int i=0;i<=n;++i)
 84         val[i]=in[i]=head[i]=is[i]=0;
 85 }
 86 void add(int from,int to)
 87 {
 88     ++tot;
 89     edge[tot].to=to;
 90 //    edge[tot].val=val;
 91     edge[tot].next=head[from];
 92     head[from]=tot;
 93 }
 94 
 95 queue<int>q;
 96 void bfs(int start)
 97 {
 98     q.push(start);
 99     while(!q.empty())
100     {
101         int now=q.front();q.pop();
102         is[now]=1;
103         for(int i=head[now];i!=0;i=edge[i].next)
104         {
105             int to=edge[i].to;
106             in[to]++;
107             if(!is[to])
108             {
109                 is[to]=1;
110                 q.push(to);
111             }
112         }
113     }
114 }
115 void topu(int x)
116 {
117     q.push(x);
118     val[x]=1;
119     while(!q.empty())
120     {
121         int now=q.front();q.pop();
122         for(int i=head[now];i!=0;i=edge[i].next)
123         {
124             int to=edge[i].to;
125             in[to]--;
126             val[to]+=val[now];
127             if(in[to]==0)
128                 q.push(to);
129         }
130     }
131 }
132 
133 void solve()
134 {
135     int n;
136     sc("%d",&n);
137     Init(n);
138     for(int i=1;i<=n;++i)
139     {
140         int J;
141         sc("%d",&J);
142         if(J==1)
143         {
144             int num;
145             sc("%d",&num);
146             for(int j=1;j<=num;++j)
147             {
148                 int t;
149                 sc("%d",&t);
150                 arr[i].push_back(t);
151             }
152         }
153         else
154         {
155             int u,v;
156             sc("%d%d",&u,&v);
157             add(i,u),add(i,v);
158         }
159     }
160     bfs(n);
161     topu(n);
162     int num=0;
163     for(int i=1;i<=n;++i)
164     {
165         if(is[i])
166         {
167             int sz=arr[i].size();
168             for(int j=0;j<sz;++j)
169                 a[++num]=arr[i][j],much[num]=val[i];
170         }
171     }
172     LS(num);
173     for(int i=1;i<=num;++i)
174         mark[i]=0;
175     for(int i=1;i<=num;++i)
176         mark[a[i]]+=much[i];
177     ll max_=0,sum=0;
178     for(int i=1;i<=num;++i)
179         max_=max(max_,mark[i]),sum+=mark[i];
180     ll ans=0;
181     if(max_>sum-max_)
182         ans=2*(sum-max_);
183     else
184         ans=sum;
185     pr("%lld\n",ans);
186     for(int i=1;i<=n;++i)
187         arr[i].clear();
188 }
189 
190 int main()
191 {
192     int T;
193     sc("%d",&T);
194     while(T--)solve();
195     return 0;
196 }
197 
198 /**************************************************************************************/
199 
200 int maxx(int a,int b)
201 {
202     return a>b?a:b;
203 }
204 
205 void swapp(int &a,int &b)
206 {
207     a^=b^=a^=b;
208 }
209 
210 int lowbit(int n)
211 {
212     return n&(-n);
213 }
214 
215 int Del_bit_1(int n)
216 {
217     return n&(n-1);
218 }
219 
220 int abss(int a)
221 {
222     return a>0?a:-a;
223 }
224 
225 double fabss(double a)
226 {
227     return a>0?a:-a;
228 }
229 
230 int minn(int a,int b)
231 {
232     return a<b?a:b;
233 }

猜你喜欢

转载自www.cnblogs.com/--HPY-7m/p/11785514.html