2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 #include #include #include #include #include #include using namespace std; int n,x,y; const int maxn=2e5+10; const int MOD1=1e9+7; const int base1=269; const int MOD2=1e9+9; const int base2=307; char c1,c2,c3,cmid; int tlist; int v,t,st,b[maxn],maxdeep[maxn],dd,max_way[maxn],f1[maxn],qq,jfll[maxn],sz_sb[maxn],ans,mar[maxn],f2[maxn],m,rvh1[maxn],rvh2[maxn],sz[maxn],sbtr[maxn]; long long pw1[maxn],pw2[maxn]; vector > ed[maxn]; vector nxt[maxn]; vector , int> > lst[maxn]; pair, int> pr[maxn]; void dfs1(int x, int y) { v++; sbtr[x]=0; sz[x]=1; b[v]=x; for (int j=0;jsbtr[x]) sbtr[x]=sz[to]; } } void dfs2(int x, int y, int h, int d, int hash1, int hash2) { if (h>0) { t++; pr[t].first=make_pair(hash1,hash2); pr[t].second=d; } maxdeep[d]=max(maxdeep[d],h); for (int j=0;jmx1) { mx2=mx1; mx1=maxdeep[i]; } else if (maxdeep[i]>mx2) mx2=maxdeep[i]; max_way[rt]=mx1+mx2; sort(pr+1,pr+t+1); int q=-1,prv=0; for (int i=1;i<=t;i++) { if (i==1 || pr[i].first!=pr[i-1].first) { q++; lst[rt].push_back(pr[i]); prv=pr[i].second; lst[rt][q].second=prv; } else if (prv!=pr[i].second) lst[rt][q].second=-1; } for (int i=0;ir) return 0; return (1ll*f1[r]-1ll*f1[l-1]*pw1[r-l+1])%MOD1; } int get_hash2(int l, int r) { if (l>r) return 0; return (1ll*f2[r]-1ll*f2[l-1]*pw2[r-l+1])%MOD2; } bool dfs3(int x, int y, int h, int d, int hash1, int hash2) { if (h>m) return false; rvh1[x]=hash1; rvh2[x]=hash2; for (int j=0;j=m/2+add) { int len_suff=m-h; int pal=0; int len_pal=(m-len_suff*2); int mid=len_pal/2; int hashf1=rvh1[b[mid]]; int hashf2=rvh2[b[mid]]; int hashs1=0,hashs2=0; if (len_pal%2==1) mid++; if (mid!=0) { hashs1=get_hash1(mid+1,len_pal); if (hashs1<0) hashs1+=MOD1; hashs2=get_hash2(mid+1,len_pal); if (hashs2<0) hashs2+=MOD2; } if (hashf1==hashs1 && hashs2==hashf2) pal=1; if (pal==1 && h==m) return true; if (pal==1) { int hash1=get_hash1(len_pal+1,h); int hash2=get_hash2(len_pal+1,h); if (hash1<0) hash1+=MOD1; if (hash2<0) hash2+=MOD2; int l=lower_bound(lst[tlist].begin(),lst[tlist].end(),make_pair(make_pair(hash1,hash2),-2))-lst[tlist].begin(); if (l!=lst[tlist].size() && lst[tlist][l].first==make_pair(hash1,hash2)) { if (lst[tlist][l].second==-1) return true; else if (lst[tlist][l].second!=d) return true; } } } return false; } bool go(int v) { mar[v]=1; tlist=v; st=0; if (max_way[v]>=m && dfs3(v,0,0,0,0,0)) return true; for (int i=0;i=1;t--) { scanf("%d",&n); if (n==1) { printf("1\n"); continue; } for (int i=1;i<=n-1;i++) { int x,y,z; scanf("%d%d%d",&x,&y,&z); ed[x].push_back(make_pair(y,z+1)); ed[y].push_back(make_pair(x,z+1)); } ans=1; int l=1; int r=n/2; int root=prebuild(1); while (r-l>0) { int mid=(r+l)/2; mid++; for (int i=1;i<=n;i++) mar[i]=0; m=mid*2; if (go(root)) l=mid; else r=mid-1; } m=l*2; for (int i=1;i<=n;i++) mar[i]=0; if (l*2>ans && go(root)) ans=l*2; l=1; r=n/2; if (n%2==0) r--; while (r-l>0) { int mid=(r+l)/2; mid++; for (int i=1;i<=n;i++) mar[i]=0; m=mid*2+1; if (go(root)) l=mid; else r=mid-1; } m=l*2+1; for (int i=1;i<=n;i++) mar[i]=0; if (l*2+1>ans && go(root)) ans=l*2+1; for (int i=1;i<=n;i++) { ed[i].clear(); lst[i].clear(); mar[i]=0; nxt[i].clear(); } printf("%d\n",ans+1); } return 0; }