Walk
Time Limit: 20 Sec Memory Limit: 256 MBDescription
Input
Output
Sample Input
3
1 2 3
1 3 9
Sample Output
9
3
0
HINT
Solution
其实吧,就是每次枚举一个d,重新构图,把权值是 d 的倍数的边加入。然后Dfs暴力求一遍直径L,显然 [1, L] 都可以用 d 更新。
重点是在于复杂度的证明吧,证明在上面qwq(BearChild当时不敢写qaq)。
Code
1 #include<iostream> 2 #include<string> 3 #include<algorithm> 4 #include<cstdio> 5 #include<cstring> 6 #include<cstdlib> 7 #include<cmath> 8 #include<vector> 9 using namespace std; 10 typedef long long s64; 11 12 const int ONE = 1000005; 13 const int INF = 2147483640; 14 15 int n; 16 int x, y, z; 17 int Maxval, S[ONE]; 18 int Ans[ONE]; 19 20 vector <int> D[ONE], G[ONE]; 21 22 struct power 23 { 24 int x, y, z; 25 }a[ONE]; 26 27 int get() 28 { 29 int res=1,Q=1;char c; 30 while( (c=getchar())<48 || c>57 ) 31 if(c=='-')Q=-1; 32 res=c-48; 33 while( (c=getchar())>=48 && c<=57 ) 34 res=res*10+c-48; 35 return res*Q; 36 } 37 38 void Dealiv() 39 { 40 for(int i = 1; i <= n; i++) 41 { 42 int Q = sqrt(a[i].z); 43 for(int j = 1; j <= Q; j++) 44 if(a[i].z % j == 0) 45 { 46 D[j].push_back(i); 47 if(a[i].z / j != j) D[a[i].z / j].push_back(i); 48 } 49 } 50 } 51 52 int vis[ONE], length, A1, Record; 53 int next[ONE], first[ONE], go[ONE], tot; 54 55 void Add(int u, int v) 56 { 57 next[++tot] = first[u]; first[u] = tot; go[tot] = v; 58 next[++tot] = first[v]; first[v] = tot; go[tot] = u; 59 } 60 61 void Dfs1(int u, int father, int dep) 62 { 63 if(length < dep) {A1 = u, length = dep;} 64 for(int e = first[u]; e; e = next[e]) 65 { 66 int v = go[e]; 67 if(v == father || vis[v]) continue; 68 Dfs1(v, u, dep + 1); 69 } 70 } 71 72 void Dfs2(int u, int father, int dep) 73 { 74 vis[u] = 1; 75 length = max(length, dep); 76 for(int e = first[u]; e; e = next[e]) 77 { 78 int v = go[e]; 79 if(v == father || vis[v]) continue; 80 Dfs2(v, u, dep + 1); 81 } 82 } 83 84 int main() 85 { 86 n = get(); 87 for(int i = 1; i < n; i++) 88 a[i].x = get(), a[i].y = get(), a[i].z = get(), Maxval = max(Maxval, a[i].z); 89 90 Dealiv(); 91 92 int res = 0; 93 94 for(int i = 1; i <= Maxval; i++) 95 { 96 int len = D[i].size(), cnt = 0; tot = 0; 97 98 for(int j = 0; j < len; j++) 99 { 100 int id = D[i][j]; 101 Add(a[id].x, a[id].y); 102 S[++cnt] = a[id].x, S[++cnt] = a[id].y; 103 } 104 105 Record = 0; 106 for(int j = 1; j <= cnt; j++) 107 if(!vis[S[j]]) 108 { 109 A1 = length = 0; Dfs1(S[j], 0, 0); 110 length = 0; Dfs2(A1, 0, 0); 111 Record = max(Record, length); 112 } 113 114 for(int j = 1; j <= cnt; j++) 115 first[S[j]] = 0, vis[S[j]] = 0; 116 117 Ans[Record] = i; 118 } 119 120 for(int i = n; i >= 1; i--) 121 Ans[i] = max(Ans[i + 1], Ans[i]); 122 123 for(int i = 1; i <= n; i++) 124 printf("%d ", Ans[i]); 125 }