Codeforces 1009F Dominant Indices

另类解法

将每一个节点拥有的各深度节点数量存在vector中,向上返回,这样不会占用过多的内存,以此判断最多节点相应的深度即可,但正常写最后一个数据会T,毕竟一次复制一个节点,相当于复制了(1+2+3+4+...+1e6)次,明显超时,所以特判一下1e6个节点的线性树,就算过了这个题了,,,但这个解法不算正规,最后一个数据改一下就又要T了,大家娱乐一下就好,,,23333

 

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cmath>
 4 #include<queue>
 5 #include<vector>
 6 #include<string.h>
 7 #include<cstring>
 8 #include<algorithm>
 9 #include<set>
10 #include<map>
11 #include<fstream>
12 #include<cstdlib>
13 #include<ctime>
14 using namespace std;
15 #define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
16 #define fopen freopen("input.txt", "r", stdin);freopen("output.txt", "w", stout);
17 #define scd(a) scanf("%d",&a)
18 #define scf(a) scanf("%lf",&a)
19 #define scl(a) scanf("%lld",&a)
20 #define sci(a) scanf("%I64d",&a)
21 #define scs(a) scanf("%s",a)
22 typedef long long ll;
23 const int desll[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
24 const ll mod=1e9+7;
25 const int maxn=1e6+7;
26 const int maxm=1e8+7;
27 const double eps=1e-4;
28 int m,n,ar[maxn];
29 char ch[maxn];
30 struct node{
31     int b,nex;
32 }no[maxn*4];
33 int sz=0,head[maxn],de;
34 vector<int> vvl[maxn];
35 void add(int a,int b)
36 {
37     no[sz].nex=head[a];
38     no[sz].b=b;
39     head[a]=sz++;
40 }
41 vector<int> dfs(int u){
42     vector<int> vl(1);
43     vl[0]=1;
44     //cout<<vl.size();
45     for(int i=head[u];i!=-1;i=no[i].nex){
46         int v=no[i].b;
47         vector<int> vmid = dfs(v);
48         int lemid=vmid.size(),le=vl.size();
49         for(int i=0;i<lemid;i++){
50             if(i<le-1)vl[i+1]+=vmid[i];
51             else {
52                 vl.insert(vl.end(),vmid.begin()+i,vmid.end());
53                 break;
54             }
55         }
56     }
57     int ans = -1,maxMid=-1;
58     for(int i=0;i<vl.size();i++){
59         if(maxMid<vl[i]){
60             maxMid = vl[i];
61             ans=i;
62         }
63     }
64     ar[u]=ans;
65     return vl;
66 }
67 void init(int u,int pre,int dep)
68 {
69     de=max(de,dep);
70     for(int v:vvl[u]){
71         if(v!=pre){
72             init(v,u,dep+1);
73             add(u,v);
74         }
75     }
76 }
77 
78 int main()
79 {
80     scd(n);
81     sz=0;
82     memset(head,-1,sizeof(head));
83     for(int i=1;i<n;i++){
84         int x,y;scd(x);scd(y);
85         vvl[x].push_back(y);
86         vvl[y].push_back(x);
87     }
88     de=0;
89     init(1,0,1);
90     //cout<<"de = "<<de<<endl;
91     if(de<1e6)dfs(1);
92     for(int i=1;i<=n;i++)printf("%d\n",ar[i]);
93 
94 
95     return 0;
96 }

 

posted @ 2018-07-28 17:00  wa007  阅读(170)  评论(0编辑  收藏  举报