|
1 |
| - ///1153 - Internet Bandwidth my code: |
2 |
| -vector <int> ed[102]; |
3 |
| -int flw[102][102],cap[102][102],par[102],rcp[102]; |
4 |
| -char vis[101]; |
5 |
| -int s,t,d; |
6 |
| -int dfs() |
7 |
| -{ |
8 |
| - queue <int> q; |
9 |
| - int v,f,i; |
10 |
| - q.push(s); |
11 |
| - MS(vis,0); |
12 |
| - vis[s]=1; |
13 |
| - rcp[s]=1E7; |
14 |
| - while(!q.empty()){ |
15 |
| - f=q.front(); |
16 |
| - q.pop(); |
17 |
| - for(i=0;i<ed[f].size();i++){ |
18 |
| - v=ed[f][i]; |
19 |
| - if(!vis[v]&&(cap[f][v]-flw[f][v]>0)){ |
20 |
| - vis[v]=1; |
21 |
| - par[v]=f; |
22 |
| - rcp[v]=min(cap[f][v]-flw[f][v],rcp[f]); |
23 |
| - if(v==d){ |
24 |
| - return 1; |
25 |
| - } |
26 |
| - q.push(v); |
27 |
| - } |
| 1 | +#include <bits/stdc++.h> |
| 2 | +#define ll long long |
| 3 | +#define P(X) cout<<"db "<<X<<endl; |
| 4 | +#define P2(X,Y) cout<<"db2 "<<X<<" "<<Y<<endl; |
| 5 | +#define rep(i,n) for(i=1;i<=n;i++) |
| 6 | +#define FO freopen("t.txt","w",stdout); |
| 7 | +#define MS(XX,YY) memset(XX,YY,sizeof(XX)); |
| 8 | +#define pii pair<int,int> |
| 9 | +#define chk(n,i) (bool)(n&(1<<i)) |
| 10 | +#define on(n,i) (n|(1<<i)) |
| 11 | +#define off(n,i) n=n&(~(1<<i)) |
| 12 | +#define eps 10e-7 |
| 13 | +#define MX 1000005 |
| 14 | +using namespace std; |
| 15 | + |
| 16 | +/** |
| 17 | +* Description : Useful for all same types of operations on each node on a tree |
| 18 | +* cnt[] storing all number of color of a type in sub tree of v |
| 19 | +* res[] storing sum of color which apears maximum numbers of time |
| 20 | +* |
| 21 | +* Time complexity : O(nlogn) |
| 22 | +* Source :https://codeforces.com/blog/entry/44351 |
| 23 | +*/ |
| 24 | + |
| 25 | + |
| 26 | +vector<int>g[MX]; |
| 27 | + |
| 28 | +int sz[MX]; |
| 29 | +void getsz(int v, int p){ |
| 30 | + sz[v] = 1; // every vertex has itself in its subtree |
| 31 | + for(auto u : g[v]) |
| 32 | + if(u != p){ |
| 33 | + getsz(u, v); |
| 34 | + sz[v] += sz[u]; // add size of child u to its parent(v) |
28 | 35 | }
|
29 |
| - } |
30 |
| - return 0; |
31 | 36 | }
|
32 | 37 |
|
| 38 | +ll cnt[MX],col[MX],ans[MX],res[MX],mxn; |
| 39 | +bool big[MX]; |
| 40 | + |
| 41 | +void add(int v, int p, int x){ |
| 42 | + //ans[cnt[col[v]]] -= col[v]; |
| 43 | + cnt[col[v]] += x; |
| 44 | + //ans[cnt[col[v]]] += col[v]; |
| 45 | + //mxn = max(cnt[col[v]],mxn); |
| 46 | + for(auto u: g[v]) |
| 47 | + if(u != p && !big[u]) |
| 48 | + add(u, v, x); |
| 49 | +} |
| 50 | + |
| 51 | + |
| 52 | +void dfs(int v, int p, bool keep){ |
| 53 | + int mx = -1, bigChild = -1; |
| 54 | + for(auto u : g[v]) |
| 55 | + if(u != p && sz[u] > mx) |
| 56 | + mx = sz[u], bigChild = u; |
| 57 | + //run a dfs on small childs and clear them from cnt |
| 58 | + for(auto u : g[v]) |
| 59 | + if(u != p && u != bigChild) |
| 60 | + dfs(u, v, 0); // |
| 61 | + // actual processing of vertex v starts from here |
| 62 | + //mxn = 0; |
| 63 | + if(bigChild != -1) |
| 64 | + dfs(bigChild, v, 1), big[bigChild] = 1; // bigChild marked as big and not cleared from cnt |
| 65 | + // calculating ans |
| 66 | + add(v, p, 1); |
| 67 | + //res[v] = ans[mxn]; |
| 68 | + /** here access the result for each node. if needed then access on add() function |
| 69 | + now cnt[c] is the number of vertices in subtree of vertex v that has color c. |
| 70 | + You can answer the queries easily. |
| 71 | + */ |
| 72 | + if(bigChild != -1) |
| 73 | + big[bigChild] = 0; |
| 74 | + if(keep == 0) |
| 75 | + add(v, p, -1); |
| 76 | +} |
| 77 | + |
| 78 | + |
| 79 | + |
33 | 80 | int main()
|
34 | 81 | {
|
35 |
| - int i,j,a,b,ts,cn=0,n,m,w,cp,cfl,tf; |
| 82 | + ll i,j,a,b,ts,cn=0,cas=0,n,m,x,y,sum=0,mn=INT_MAX,u,v; |
36 | 83 | //freopen("test.txt","r",stdin);
|
37 |
| - scanf("%d",&ts); |
38 |
| - while(ts--){ |
39 |
| - scanf("%d %d %d %d",&n,&s,&d,&m); |
40 |
| - for(i=0;i<=n;i++){ |
41 |
| - ed[i].clear(); |
42 |
| - } |
43 |
| - MS(cap,0) |
44 |
| - for(i=0;i<m;i++){ |
45 |
| - scanf("%d %d %d",&a,&b,&w); |
46 |
| - if(w==0)continue; |
47 |
| - if(!cap[a][b]){ |
48 |
| - ed[a].push_back(b); |
49 |
| - ed[b].push_back(a); |
50 |
| - } |
51 |
| - cap[a][b]+=w; |
52 |
| - cap[b][a]+=w; |
53 |
| - } |
54 |
| - tf=0; |
55 |
| - MS(flw,0); |
56 |
| - while(dfs()){ |
57 |
| - cp=d; |
58 |
| - cfl=rcp[d]; |
59 |
| - tf+=cfl; |
60 |
| - //P(tf) |
61 |
| - do{ |
62 |
| - flw[par[cp]][cp]+=cfl; |
63 |
| - flw[cp][par[cp]]-=cfl; |
64 |
| - cp=par[cp]; |
65 |
| - }while(cp!=s); |
66 |
| - } |
67 |
| - printf("Case %d: %d\n",++cn,tf); |
| 84 | + cin>>n; |
| 85 | + for(int i = 1; i <= n; i++) { |
| 86 | + cin >> col[i]; |
| 87 | + } |
| 88 | + for(i=1;i<n;i++){ |
| 89 | + cin>>u>>v; |
| 90 | + g[u].push_back(v); |
| 91 | + g[v].push_back(u); |
68 | 92 | }
|
| 93 | + getsz(1,0); |
| 94 | + dfs(1,0,1); |
| 95 | + for(int i = 1; i <= n; i++) { |
| 96 | + cout << res[i] << ' '; |
| 97 | + } |
69 | 98 | return 0;
|
70 | 99 | }
|
0 commit comments