solution
stringlengths
11
983k
difficulty
int64
0
21
language
stringclasses
2 values
#include <bits/stdc++.h> using namespace std; const long long M = 1e9 + 7; int32_t main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n; cin >> n; string s; cin >> s; long long c1 = 0, c2 = 0, cnt1 = 0, cnt2 = 0; for (long long i = 0; i < n; i++) { if (i % 2 == 0) { if (s[i] == 'r') continue; cnt1++; } else { if (s[i] == 'b') continue; cnt2++; } } c1 = max(cnt1, cnt2); cnt1 = 0; cnt2 = 0; for (long long i = 0; i < n; i++) { if (i % 2 == 0) { if (s[i] == 'b') continue; cnt1++; } else { if (s[i] == 'r') continue; cnt2++; } } c2 = max(cnt1, cnt2); cout << min(c1, c2); }
8
CPP
#include <bits/stdc++.h> using namespace std; int n, m, k, l, i, a1, a2, b1, b2, res; string s; int main() { cin >> n >> s; for (i = 0; i < n; i++) { if (s[i] == 'b') if (i % 2 == 0) a1++; else a2++; else if (i % 2 == 0) b1++; else b2++; } res = min(max(a2, b1), max(a1, b2)); cout << res; }
8
CPP
def calc(pat): nR = nB = 0 for i in range(n): if s[i] != pat[i]: if s[i] == 'b': nB += 1 else: nR += 1 return max(nR, nB) n = int(input()) s = list(input()) r = 'rb' * (n // 2) +'r' * (n & 1) b = 'br' * (n // 2) +'b' * (n & 1) print(min(calc(r), calc(b)))
8
PYTHON3
n=input() text=input() switcher=-1 #b=1 #r=-1 lastbad=0 tries=10000000 triecount=0 triecount2=0 counter=-1 abad1=0 abad2=0 for a in text: counter+=1 switcher*=-1 if a=='r' and switcher==-1: abad1+=1 if a=='b' and switcher==1: abad2+=1 if abad1>=abad2: triecount=abad1 else: triecount=abad2 if triecount<tries: tries==triecount tc1=triecount switcher=-1 abad1=0 abad2=0 for a in text: counter+=1 switcher*=-1 if a=='b' and switcher==-1: abad1+=1 if a=='r' and switcher==1: abad2+=1 if abad1>=abad2: triecount2=abad1 else: triecount2=abad2 #print('tc1-'+str(triecount)+'tc2-'+str(triecount2)) tc1=triecount tc2=triecount2 if tc1<=tc2: print(tc1) else: print(tc2)
8
PYTHON3
#!/usr/bin/env python3 # -*- coding: utf-8 -*- def f(first, s): second = 'r' if first == 'b' else 'b' alt = [first, second] error = [0, 0] for i,ch in enumerate(s): shouldbe = alt[i % 2] if ch != shouldbe: error[i % 2] += 1 return max(error) def main(): n = int(input()) s = input() print(min(f('r', s), f('b', s))) main()
8
PYTHON3
n = int(input()) colors = list(input()) red_first = 0 red_second = 0 black_first = 0 black_second = 0 for i in range(0, n): if colors[i] is 'r': if i % 2 is 0: red_second += 1 else: red_first += 1 elif colors[i] is 'b': if i % 2 is 0: black_second += 1 else: black_first += 1 red = red_first + red_second black = black_first + black_second result = 0 if red > black: if not red_first > red_second: result = max(red_first, black_second) else: result = max(red_second, black_first) else: if not black_first > black_second: result = max(black_first, red_second) else: result = max(black_second, red_first) print(result)
8
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int n, i, k, l, p, m; string str; cin >> n; cin >> str; int blceven = 0; int blcodd = 0; int redeven = 0; int redodd = 0; for (i = 0; i < n; i++) { if (i % 2 == 0) { if (str[i] == 'b') { blceven++; } else { redeven++; } } else { if (str[i] == 'b') { blcodd++; } else redodd++; } } int ans1; int ans2; if (redeven == blcodd) { ans1 = redeven; } else { ans1 = min(redeven, blcodd) + abs(redeven - blcodd); } if (redodd == blceven) { ans2 = redodd; } else { ans2 = min(redodd, blceven) + abs(redodd - blceven); } cout << min(ans1, ans2) << endl; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int ans, n, b, r, s, t, ans2; char c[100009], k, f[100009]; int main() { cin >> n; for (int i = 1; i <= n; i++) cin >> c[i], f[i] = c[i]; for (int i = 1; i <= n; i++) { if (i % 2 == 1 and c[i] != 'r') t++; if (i % 2 == 0 and c[i] == 'r') s++; } ans += min(t, s); ans += abs(t - s); t = 0; s = 0; for (int i = 1; i <= n; i++) { if (i % 2 == 1 and f[i] != 'b') t++; if (i % 2 == 0 and f[i] == 'b') s++; } ans2 += min(t, s); ans2 += abs(t - s); cout << min(ans, ans2); }
8
CPP
#include <bits/stdc++.h> using namespace std; const int maxn = 100000 + 10; const double eps = 1e-10; const int inf = 0x3f3f3f3f; const double pi = acos(-1.0); char s[maxn]; int main() { int n; scanf("%d", &n); scanf("%s", s + 1); int ans = inf; int sumr2 = 0; int sumb1 = 0; for (int i = 1; i <= n; ++i) { if (s[i] == 'r' && !(i & 1)) sumr2++; if (s[i] == 'b' && (i & 1)) sumb1++; } ans = min(ans, min(sumr2, sumb1) + max(sumr2, sumb1) - min(sumr2, sumb1)); int sumb2 = 0, sumr1 = 0; for (int i = 1; i <= n; ++i) { if (s[i] == 'r' && (i & 1)) sumr1++; if (s[i] == 'b' && !(i & 1)) sumb2++; } ans = min(ans, max(sumr1, sumb2)); printf("%d\n", ans); return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int main() { int n, ans1 = 0, b1 = 0, r1 = 0, b2 = 0, r2 = 0, ans2 = 0; string s; cin >> n >> s; for (int i = 0; i < n; i += 2) { b1 += s[i] != 'b'; r2 += s[i] != 'r'; } for (int i = 1; i < n; i += 2) { b2 += s[i] != 'b'; r1 += s[i] != 'r'; } ans1 = min(b1, r1) + (max(b1, r1) - min(b1, r1)); ans2 = min(b2, r2) + (max(b2, r2) - min(b2, r2)); cout << min(ans1, ans2); return 0; }
8
CPP
n=input() word=input() c=0 sum1,sum2,sum3,sum4=0,0,0,0 for i in word: if c==0 : if i=='r': sum1+=1 else : sum2+=1 c=1 else : if i=='b': sum3+=1 else : sum4+=1 c=0 print(min(max(sum1,sum3),max(sum2,sum4)))
8
PYTHON3
am = int(input()) arr = list(input()) for i in range(am): if arr[i] == "r": arr[i] = 1 else: arr[i] = 0 ans = 0 # print(arr) # First case: 01010101 onesWrong = 0 nullsWrong = 0 for i in range(am): if arr[i]!=i%2: if arr[i]: onesWrong+=1 else: nullsWrong+=1 # print(onesWrong) # print(nullsWrong) ans = min(onesWrong,nullsWrong) + (max(onesWrong,nullsWrong) - min(onesWrong,nullsWrong)) # print('----') # Second case: 10101010 onesWrong = 0 nullsWrong = 0 for i in range(am): if arr[i] != (i%2 + 1) % 2: if arr[i]: onesWrong+=1 else: nullsWrong+=1 # print(onesWrong) # print(nullsWrong) ans = min(min(onesWrong,nullsWrong) + (max(onesWrong,nullsWrong) - min(onesWrong,nullsWrong)),ans) print(ans)
8
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int n, c1, c2; char s[100010]; while (cin >> n) { cin >> s; c1 = 0; c2 = 0; for (int i = 0; i < n; i++) if (i % 2 == 0) { if (s[i] != 'r') c1++; } else { if (s[i] != 'b') c2++; } int ans1 = abs(c1 - c2) + min(c1, c2); c1 = 0; c2 = 0; for (int i = 0; i < n; i++) if (i % 2 == 0) { if (s[i] != 'b') c1++; } else { if (s[i] != 'r') c2++; } int ans2 = abs(c1 - c2) + min(c1, c2); cout << min(ans1, ans2) << endl; } }
8
CPP
n = int(input()) s = input() x, y = 0, 0 def solve(a, b): x, y = 0, 0 for i in range(n): if i%2==0: if s[i]!=a: x += 1 else: if s[i]!=b: y += 1 return max(x, y) ans = solve('r', 'b') ans = min(ans, solve('b', 'r')) print(ans)
8
PYTHON3
n = int(input()) a = [[0, 0] for _ in range(2)] for i, c in enumerate(input().rstrip()): a[i & 1][c == 'r'] += 1 print(min(max(a[0][0], a[1][1]), max(a[1][0], a[0][1])))
8
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n; cin >> n; string s; int r[2] = {}, b[2] = {}; cin >> s; for (int i = 0; i < n; i++) { if (i % 2 == 0) { if (s.at(i) == 'r') r[0]++; else b[0]++; } else { if (s.at(i) == 'r') r[1]++; else b[1]++; } } int rtot, btot; if (r[0] > r[1]) rtot = r[0]; else rtot = r[1]; if (b[0] > b[1]) rtot = b[0]; else rtot = b[1]; if (rtot > btot) { if (r[0] > r[1]) { if (r[1] > b[0]) cout << r[1]; else cout << b[0]; } else { if (b[1] > r[0]) cout << b[1]; else cout << r[0]; } } else { if (b[0] > b[1]) { if (b[1] > r[0]) cout << b[1]; else cout << r[0]; } else { if (b[0] > r[1]) cout << b[0]; else cout << r[1]; } } return 0; }
8
CPP
import sys input = sys.stdin.readline from collections import defaultdict opp = {"r":"b","b":"r"} def func(n,a,want,d): curr = defaultdict(int) ans = 0 for i in range(n): if a[i]!=want[i]: if curr[d[a[i]]]>0: curr[d[a[i]]]-=1 else: curr[a[i]]+=1 ans+=1 return ans n = int(input()) a = input() want1 = "r" want2 = "b" for i in range(1,n): want1+=opp[want1[i-1]] want2+=opp[want2[i-1]] ans = func(n,a,want1,opp) ans = min(ans,func(n,a,want2,opp)) print(ans)
8
PYTHON3
# def I(): return(list(map(int,input().split()))) def solve(arr): # print(arr) f=min(arr.count("b"),arr.count("r")) return len(arr)-f n=int(input()) s=input() accs0=[] accs1=[] for i in range(n): if i%2==1: if s[i]=='r': accs0.append(s[i]) else: accs1.append(s[i]) else: if s[i]!='r': accs0.append(s[i]) else: accs1.append(s[i]) print(min(solve(accs1),solve(accs0)))
8
PYTHON3
#n=int(input()) #mas1=input() #from random import randrange #n=10 #mas1="br"*n #print(mas1) #delta=randrange(0,n*2) n=int(input()) mas1=input() mas=[] for i in range(n): mas.append(mas1[i]) ############################################ def calc (t): #t="b" #switch v=[] res=[] g=0 for i in range(n): if mas[i]!=t: v.append(mas[i]) g=1 elif g==1: res.append(v) v=[] g=0 if t=="r": #0 t="b" #1 else: t="r" #0 res.append(v) S=0 res1=[] for i in range(len(res)): t=len(res[i]) if t%2!=0: p=res[i].count("r") #0 w=res[i].count("b") #1 if p>w: res1.append("r") #0 else: res1.append("b") #1 S=S+t//2 p=res1.count("r") #0 w=res1.count("b") #b if p>w: S=S+p else: S=S+w return S ############################################ S1=calc("b") S=calc("r") if S1<S: print(S1) else: print(S)
8
PYTHON3
#include <bits/stdc++.h> using namespace std; int n, l0, r0, r1, l1; char a; int main() { cin >> n; for (int i = 0; i < n; i++) { cin >> a; if (i % 2 == 0) { if (a == 'r') r0++; else l1++; } else { if (a == 'r') r1++; else l0++; } } cout << min(max(l0, r0), max(l1, r1)); }
8
CPP
n = int(input()) s = input() cwred = 0 cwblack = 0 res = 0 #red first for i in range(n): if i % 2 == 0 and s[i] == 'b': cwblack += 1 if i % 2 == 1 and s[i] == 'r': cwred += 1 res = min(cwblack, cwred) + abs(cwblack - cwred) cwred = 0 cwblack = 0 #black first for i in range(n): if i % 2 == 1 and s[i] == 'b': cwblack += 1 if i % 2 == 0 and s[i] == 'r': cwred += 1 if res > min(cwblack, cwred) + abs(cwblack - cwred): res = min(cwblack, cwred) + abs(cwblack - cwred) print(res)
8
PYTHON3
#include <bits/stdc++.h> using namespace std; int n, m, a[1000100]; int main() { int r, k, i, c = 0, x = 0, y = 0, j, t, l, z, x1 = 0, y1 = 0; long long ans = 0; string p; scanf("%d", &n); cin >> p; for (i = 0; i < n; i++) { if (i % 2 == 0) { if (p[i] != 'r') x++; else x1++; } else { if (p[i] != 'b') y++; else y1++; } } cout << min(max(x, y), max(x1, y1)) << endl; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int main() { long n, r1, r2, b1, b2; char x; cin >> n; r1 = r2 = b1 = b2 = 0; for (long i = 0; i < n; i++) { cin >> x; if (x == 'r') { if ((i & 1) == 0) r1++; else r2++; } else { if ((i & 1) == 0) b1++; else b2++; } } cout << min(max(r1, b2), max(r2, b1)); }
8
CPP
#include <bits/stdc++.h> using namespace std; int func1(string input) { char c = 'r'; int rr = 0, rb = 0, br = 0, bb = 0; for (int i = 0; i < input.length(); i++) { if (input[i] == 'r' && c == 'r') rr++; else if (input[i] == 'r' && c == 'b') rb++; else if (input[i] == 'b' && c == 'r') br++; else if (input[i] == 'b' && c == 'b') bb++; if (c == 'r') c = 'b'; else c = 'r'; } return max(rb, br); } int func2(string input) { char c = 'b'; int rr = 0, rb = 0, br = 0, bb = 0; for (int i = 0; i < input.length(); i++) { if (input[i] == 'r' && c == 'r') rr++; else if (input[i] == 'r' && c == 'b') rb++; else if (input[i] == 'b' && c == 'r') br++; else if (input[i] == 'b' && c == 'b') bb++; if (c == 'r') c = 'b'; else c = 'r'; } return max(rb, br); } int main() { int len; string input; cin >> len >> input; cout << min(func1(input), func2(input)) << endl; }
8
CPP
def solve(): n = int(input()) s = input() ans1b = 0 ans1r = 0 ans2b = 0 ans2r = 0 for i, c in enumerate(s): if i % 2 == 0: if c == 'b': ans1r += 1 else: ans2b += 1 else: if c == 'r': ans1b += 1 else: ans2r += 1 print(min(max(ans1b, ans1r), max(ans2b, ans2r))) def main(): solve() if __name__ == '__main__': main()
8
PYTHON3
#include <bits/stdc++.h> using namespace std; int n; char cad[100010]; int fun(string s) { int cr = 0, cb = 0, cnt = 0; for (int i = 0; i < n; i++) { if (cad[i] != s[i % 2]) { if (s[i % 2] == 'r') { if (cr > 0) cr--; else { cb++; cnt++; } } else if (s[i % 2] == 'b') { if (cb > 0) cb--; else { cr++; cnt++; } } } } return cnt; } int main() { scanf("%d", &n); scanf("%s", cad); int resp = fun("br"); resp = min(resp, fun("rb")); printf("%d\n", resp); return 0; }
8
CPP
n = int(input()) s = input() l1 = "br" * (n // 2) + "b" * (n % 2) l2 = "rb" * (n // 2) + "r" * (n % 2) b1 = [0] * ((n // 2) + (n % 2)) b2 = [0] * (n // 2) r1 = [0] * (n // 2) r2 = [0] * ((n // 2) + (n % 2)) counter1 = 0 counter2 = 0 m1 = 0 m2 = 0 k1 = 0 k2 = 0 for i in range(n): if l1[i] == s[i]: if i % 2 == 0: b1[m1] = 1 m1 += 1 else: r1[k1] = 1 k1 += 1 if l2[i] == s[i]: if i % 2 == 0: r2[m2] = 1 m2 += 1 else: b2[k2] = 1 k2 += 1 a1 = b1.count(0) a2 = r1.count(0) counter1 = abs(a2 - a1) + min(a1,a2) a1 = b2.count(0) a2 = r2.count(0) counter2 = abs(a2 - a1) + min(a1,a2) print(min(counter1, counter2))
8
PYTHON3
N = int(input()) S = input() # first: r not_r = 0 not_b = 0 for i, s in enumerate(S): if i % 2 == 0: if s != "r": not_r += 1 else: if s != "b": not_b += 1 ans = max(not_r, not_b) # first: b not_r = 0 not_b = 0 for i, s in enumerate(S): if i % 2 == 0: if s != "b": not_b += 1 else: if s != "r": not_r += 1 ans = min(ans, max(not_r, not_b)) print(ans)
8
PYTHON3
n=int(input()) s=input() c=1 c1r=0;c1b=0;c2r=0;c2b=0 for x in range(0,len(s)): if c==1: if s[x]=='b': c1r=c1r+1 else: c2b=c2b+1 c=2 else: if s[x]=='r': c1b=c1b+1 else: c2r=c2r+1 c=1 print(min(max(c1r,c1b),max(c2r,c2b)))
8
PYTHON3
#include <bits/stdc++.h> using namespace std; void solve() { long long int n, m, x, y, i, j, ans = 0, c = 0, sum = 0; string s; cin >> n >> s; for (auto ch : s) if (ch == 'r') c++; if (c == 0 || c == n) { cout << n / 2 << "\n"; return; } long long int cr = 0, cb = 0; for (i = 0; i < n; i++) { if (i % 2 == 0) if (s[i] == 'r') cr++; if (i % 2 != 0) if (s[i] == 'b') cb++; } ans = max(cr, cb); cr = 0, cb = 0; for (i = 0; i < n; i++) { if (i % 2 != 0) if (s[i] == 'r') cr++; if (i % 2 == 0) if (s[i] == 'b') cb++; } ans = min(ans, max(cr, cb)); cout << ans << "\n"; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t; t = 1; while (t--) { solve(); } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; char c[100010]; int r, b; int main() { int n; scanf("%d", &n); for (int i = 0; i < n; i++) cin >> c[i]; int cnt = 0; for (int i = 0; i < n; i++) { if (i % 2 && c[i] == 'r') r++; if (i % 2 == 0 && c[i] == 'b') b++; } cnt = max(r, b); r = 0, b = 0; for (int i = 0; i < n; i++) { if (i % 2 && c[i] == 'b') r++; if (i % 2 == 0 && c[i] == 'r') b++; } cnt = min(max(r, b), cnt); printf("%d\n", cnt); }
8
CPP
n=int(input()) s=input() e=s[::2] o=s[1::2] c=n for i in range(2): if i==0: a=len(e)-e.count('r') b=len(o)-o.count('b') else: a=len(e)-e.count('b') b=len(o)-o.count('r') c=min(c,max(a,b)) print(c)
8
PYTHON3
#include <bits/stdc++.h> using namespace std; const double pi = acos(-1.0); double ax, ay, bx, by; double dis(double x, double y) { return sqrt(x * x + y * y); } double get(double a, double w) { double v = 0; int num = 100; for (int i = 0; i <= 100; i++) { double x = ax + (bx - ax) * i / (double)num; double y = ay + (by - ay) * i / (double)num; double p = atan2(y, x) - a; if (p > 2 * pi) p -= 2 * pi; while (p < 0) p += 2 * pi; if (p > 2 * pi - p) p = 2 * pi - p; double t = ((w < 1e-5) ? (i == 0 ? 0 : 1e10) : p / w); v = max(v, hypot(x - ax, y - ay) / t); } return v; } double x[10010], y[10010], a[10010], w[10010]; int main() { int n, k; while (scanf("%lf%lf%lf%lf", &ax, &ay, &bx, &by) != EOF) { scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%lf%lf%lf%lf", &x[i], &y[i], &a[i], &w[i]); vector<double> v; v.clear(); for (int i = 0; i < n; i++) { ax -= x[i]; ay -= y[i]; bx -= x[i]; by -= y[i]; double vv = get(a[i], w[i]); v.push_back(vv); ax += x[i]; ay += y[i]; bx += x[i]; by += y[i]; } sort(v.begin(), v.end()); reverse(v.begin(), v.end()); scanf("%d", &k); if (k >= v.size()) printf("0.00000\n"); else printf("%.5lf\n", v[k]); } return 0; }
12
CPP
#include <bits/stdc++.h> using namespace std; const int MAXN = 10000; int n; double x1, y3223789, x2, y2; double x[MAXN], y[MAXN], w[MAXN], a0[MAXN]; double t1[MAXN], t2[MAXN], d[MAXN], p[MAXN]; double pi = 2 * acos(0.0); double cross(double x1, double y3223789, double x2, double y2) { return x1 * y2 - x2 * y3223789; } double getmin(double x) { while (x < 0) x += 2 * pi; while (x > 2 * pi) x -= 2 * pi; return min(x, 2 * pi - x); } double get_time(int i, double xx, double yy) { double ang = atan2(yy - y[i], xx - x[i]) - a0[i]; return getmin(ang) / w[i]; } double inter(double x1, double y3223789, double x2, double y2, double x3, double y3, double x4, double y4) { double d = cross(x2 - x1, y2 - y3223789, x3 - x4, y3 - y4); double p = cross(x2 - x1, y2 - y3223789, x3 - x1, y3 - y3223789); return p / d; } double nt; int check(int i, double v) { if (get_time(i, x2, y2) < nt) return 1; if (p[i] < 0) return 0; double l = 0, r = min(1.0, p[i]); for (int o = 0; o < 50; o++) { double c1 = (l * 2 + r) / 3; double c2 = (l + 2 * r) / 3; double cx1 = x1 * (1 - c1) + x2 * c1; double cy1 = y3223789 * (1 - c1) + y2 * c1; double cx2 = x1 * (1 - c2) + x2 * c2; double cy2 = y3223789 * (1 - c2) + y2 * c2; double t1 = get_time(i, cx1, cy1); double t2 = get_time(i, cx2, cy2); t1 -= nt * c1; t2 -= nt * c2; if (t1 < 0 || t2 < 0) return 1; if (t1 < t2) r = c2; else l = c1; } return 0; } int getans(double v) { int ans = 0; nt = sqrt((x2 - x1) * (x2 - x1) + (y2 - y3223789) * (y2 - y3223789)) / v; for (int i = 0; i < n; i++) if (check(i, v)) ans++; return ans; } int main() { cin >> x1 >> y3223789 >> x2 >> y2; double dist = sqrt((x2 - x1) * (x2 - x1) + (y2 - y3223789) * (y2 - y3223789)); cin >> n; for (int i = 0; i < n; i++) { cin >> x[i] >> y[i] >> a0[i] >> w[i]; double tmp = cross(x[i] - x1, y[i] - y3223789, x[i] - x2, y[i] - y2); t1[i] = get_time(i, x1, y3223789); t2[i] = get_time(i, x2, y2); p[i] = inter(x[i], y[i], x[i] + y2 - y3223789, y[i] + x1 - x2, x1, y3223789, x2, y2); d[i] = abs(tmp) / dist; } int k; cin >> k; double l = 0, r = 4000000; while (r - l > 1e-5) { double v = (l + r) / 2; if (getans(v) > k) l = v; else r = v; } printf("%.4lf", (l + r) / 2); return 0; }
12
CPP
#include <bits/stdc++.h> using namespace std; double cal_ang(double x, double y) { double res = acos(abs(x / hypot(x, y))); if (x <= 0 && y >= 0) return acos(-1) - res; if (x <= 0 && y < 0) return acos(-1) + res; if (x > 0 && y < 0) return acos(-1) * 2.0 - res; return res; } int main() { double x0, x1, y0, y1, t0, t1, t2, t3; int n, k; vector<double> time; scanf("%lf%lf%lf%lf%d", &x0, &y0, &x1, &y1, &n); for (long long i = 0; i < ((long long)n); i++) { scanf("%lf%lf%lf%lf", &t0, &t1, &t2, &t3); if (t3 < 1e-5) continue; x0 -= t0; x1 -= t0; y0 -= t1; y1 -= t1; double tmp = 0; for (long long j = 0; j < ((long long)100 + 1); j++) { double x = x0 + (x1 - x0) * j / (double)100; double y = y0 + (y1 - y0) * j / (double)100; double ang = cal_ang(x, y); ang -= t2; if (ang < 0) ang += 2.0 * acos(-1); while (ang > acos(-1) * 2.0) ang -= acos(-1) * 2.0; ang = min(acos(-1) * 2.0 - ang, ang); tmp = max(tmp, hypot(x0 - x, y0 - y) / (ang / t3)); } time.push_back(tmp); x0 += t0; x1 += t0; y0 += t1; y1 += t1; } scanf("%d", &k); if (((long long)time.size()) <= k) { printf("0.0000\n"); return 0; } sort(time.begin(), time.end()); reverse(time.begin(), time.end()); printf("%.4lf\n", time[k]); }
12
CPP
#include <bits/stdc++.h> using namespace std; const double pi = acos(-1.00), eps = 1e-9; struct point { double x, y, w, a; }; point st, en, tank[11111]; int n, k; void norm(double &theta) { while (theta < -pi + eps) theta += 2 * pi; while (theta > pi + eps) theta -= 2 * pi; } bool check(double v) { int i, j, s, p, q, nk = 0, it; double now_t, theta, dist, T, w; double ltheta, low, high, mid, mt, vs, fs, bs, mx, my, fx, fy; bool ft; dist = sqrt((en.x - st.x) * (en.x - st.x) + (en.y - st.y) * (en.y - st.y)); T = dist / v; for (i = 0; i < n; i++) { theta = atan2(en.y - tank[i].y, en.x - tank[i].x); while (theta < tank[i].a - eps) theta += 2 * pi; theta -= tank[i].a; if (theta > pi) theta = 2 * pi - theta; now_t = theta / tank[i].w; if (v * now_t < sqrt((en.x - st.x) * (en.x - st.x) + (en.y - st.y) * (en.y - st.y))) { nk++; continue; } ltheta = atan2(st.y - tank[i].y, st.x - tank[i].x); if (tank[i].w == 0) continue; else { while (ltheta < tank[i].a - eps) ltheta += 2 * pi; ltheta -= tank[i].a; w = tank[i].w; if (ltheta > pi) { w = -w; ltheta = 2 * pi - ltheta; } theta = atan2(en.y - st.y, en.x - st.x); low = ltheta / tank[i].w; high = T; if (low > high) continue; mt = low; fs = (st.x + v * cos(theta) * mt - tank[i].x) * sin(tank[i].a + w * mt) - (st.y + v * sin(theta) * mt - tank[i].y) * cos(tank[i].a + w * mt); mt = high; bs = (st.x + v * cos(theta) * mt - tank[i].x) * sin(tank[i].a + w * mt) - (st.y + v * sin(theta) * mt - tank[i].y) * cos(tank[i].a + w * mt); if ((fs < eps && bs > -eps) || (fs > -eps && bs < eps)) { nk++; continue; } for (it = 0; it < 50; it++) { mx = mt = (2 * low + high) / 3.; fx = (st.x + v * cos(theta) * mt - tank[i].x) * sin(tank[i].a + w * mt) - (st.y + v * sin(theta) * mt - tank[i].y) * cos(tank[i].a + w * mt); my = mt = (low + 2 * high) / 3.; fy = (st.x + v * cos(theta) * mt - tank[i].x) * sin(tank[i].a + w * mt) - (st.y + v * sin(theta) * mt - tank[i].y) * cos(tank[i].a + w * mt); if ((fs > 0 && fx > fy) || (fs < 0 && fx < fy)) low = mx; else high = my; } mt = 0.5 * (low + high); fx = (st.x + v * cos(theta) * mt - tank[i].x) * sin(tank[i].a + w * mt) - (st.y + v * sin(theta) * mt - tank[i].y) * cos(tank[i].a + w * mt); if ((fs > 0 && fx < eps) || (fs < 0 && fx > -eps)) { nk++; continue; } } } return nk <= k; } int main() { int i, j, s, p, q; double low, high, mid; scanf("%lf%lf%lf%lf", &st.x, &st.y, &en.x, &en.y); scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%lf%lf%lf%lf", &tank[i].x, &tank[i].y, &tank[i].a, &tank[i].w); norm(tank[i].a); } scanf("%d", &k); low = 0; high = 1e9; while (low < high - eps) { mid = (low + high) * 0.5; if (check(mid)) high = mid; else low = mid + eps; } printf("%.20f\n", (low + high) * 0.5); return 0; }
12
CPP
#include <bits/stdc++.h> using namespace std; template <class T> inline void read(T& x) { bool fu = 0; char c; for (c = getchar(); c <= 32; c = getchar()) ; if (c == '-') fu = 1, c = getchar(); for (x = 0; c > 32; c = getchar()) x = x * 10 + c - '0'; if (fu) x = -x; }; template <class T> inline void read(T& x, T& y) { read(x); read(y); } template <class T> inline void read(T& x, T& y, T& z) { read(x); read(y); read(z); } inline char getc() { char c; for (c = getchar(); c <= 32; c = getchar()) ; return c; } const double pi = 2 * acos(0); struct node { double x, y; }; void read(node& a) { read(a.x, a.y); } int sgn(double x) { if (x == 0) return 0; return x > 0 ? 1 : -1; } node operator+(node a, node b) { return (node){a.x + b.x, a.y + b.y}; } node operator-(node a, node b) { return (node){a.x - b.x, a.y - b.y}; } node operator*(node a, double x) { return (node){a.x * x, a.y * x}; } node operator/(node a, double x) { return (node){a.x / x, a.y / x}; } node mul(node a, node b) { return (node){a.x * b.x, a.y * b.y}; } double operator*(node a, node b) { return a.x * b.y - a.y * b.x; } double xj(node a, node b, node c) { return (b - a) * (c - a); } double len(node a) { return sqrt(a.x * a.x + a.y * a.y); } bool on(node a, node b, node c) { if (a.x >= min(b.x, c.x) && a.x <= max(b.x, c.x) && a.y >= min(b.y, c.y) && a.y <= max(b.y, c.y) && xj(b, a, c) == 0) return 1; return 0; } bool pdj(node a, node b, node c, node d) { return sgn(xj(a, c, b)) * sgn(xj(a, b, d)) > 0 && sgn(xj(b, c, a)) * sgn(xj(b, a, d)) > 0 && sgn(xj(c, b, d)) * sgn(xj(c, d, a)) > 0 && sgn(xj(d, a, c)) * sgn(xj(d, c, b)) > 0; } int n, i, j, k, p, K; double L, R; node A, B, C, D; double alp, w, a[11111]; const int W = 2000; int main() { read(A); read(B); read(n); for (i = 1; i <= n; i++) { read(C); scanf("%lf%lf", &alp, &w); double vmax = 0, z; for (int cnt = (0); cnt <= (W); cnt++) { D = (node){(A.x + (B.x - A.x) / W * cnt), (A.y + (B.y - A.y) / W * cnt)}; z = alp - atan2(D.y - C.y, D.x - C.x); while (z < 0) z += 2 * pi; while (z >= 2 * pi) z -= 2 * pi; if (z > pi) z = 2 * pi - z; vmax = max(vmax, len(D - A) / (z / w + 1e-12)); } a[i] = vmax; } read(K); sort(a + 1, a + 1 + n); cout << fixed << setprecision(10) << a[n - K] << endl; return 0; }
12
CPP
#include <bits/stdc++.h> using namespace std; const double pi = acos(-1.00), eps = 1e-9; struct point { double x, y, w, a; }; point st, en, tank[11111]; int n, k; void norm(double &theta) { while (theta < -pi + eps) theta += 2 * pi; while (theta > pi + eps) theta -= 2 * pi; } bool check(double v) { int i, j, s, p, q, nk = 0, it; double now_t, theta, dist, T, w; double ltheta, low, high, mid, mt, vs, fs, bs, mx, my, fx, fy; bool ft; dist = sqrt((en.x - st.x) * (en.x - st.x) + (en.y - st.y) * (en.y - st.y)); T = dist / v; for (i = 0; i < n; i++) { theta = atan2(en.y - tank[i].y, en.x - tank[i].x); while (theta < tank[i].a - eps) theta += 2 * pi; theta -= tank[i].a; if (theta > pi) theta = 2 * pi - theta; now_t = theta / tank[i].w; if (v * now_t < sqrt((en.x - st.x) * (en.x - st.x) + (en.y - st.y) * (en.y - st.y))) { nk++; continue; } ltheta = atan2(st.y - tank[i].y, st.x - tank[i].x); if (tank[i].w == 0) continue; else { while (ltheta < tank[i].a - eps) ltheta += 2 * pi; ltheta -= tank[i].a; w = tank[i].w; if (ltheta > pi) { w = -w; ltheta = 2 * pi - ltheta; } theta = atan2(en.y - st.y, en.x - st.x); low = ltheta / tank[i].w; high = T; if (low > high) continue; mt = low; fs = (st.x + v * cos(theta) * mt - tank[i].x) * sin(tank[i].a + w * mt) - (st.y + v * sin(theta) * mt - tank[i].y) * cos(tank[i].a + w * mt); mt = high; bs = (st.x + v * cos(theta) * mt - tank[i].x) * sin(tank[i].a + w * mt) - (st.y + v * sin(theta) * mt - tank[i].y) * cos(tank[i].a + w * mt); if ((fs < eps && bs > -eps) || (fs > -eps && bs < eps)) { nk++; continue; } for (it = 0; it < 50; it++) { mx = mt = (2 * low + high) / 3.; fx = (st.x + v * cos(theta) * mt - tank[i].x) * sin(tank[i].a + w * mt) - (st.y + v * sin(theta) * mt - tank[i].y) * cos(tank[i].a + w * mt); my = mt = (low + 2 * high) / 3.; fy = (st.x + v * cos(theta) * mt - tank[i].x) * sin(tank[i].a + w * mt) - (st.y + v * sin(theta) * mt - tank[i].y) * cos(tank[i].a + w * mt); if ((fs > 0 && fx > fy) || (fs < 0 && fx < fy)) low = mx; else high = my; } mt = 0.5 * (low + high); fx = (st.x + v * cos(theta) * mt - tank[i].x) * sin(tank[i].a + w * mt) - (st.y + v * sin(theta) * mt - tank[i].y) * cos(tank[i].a + w * mt); if ((fs > 0 && fx < eps) || (fs < 0 && fx > -eps)) { nk++; continue; } } } return nk <= k; } int main() { int i, j, s, p, q; double low, high, mid; scanf("%lf%lf%lf%lf", &st.x, &st.y, &en.x, &en.y); scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%lf%lf%lf%lf", &tank[i].x, &tank[i].y, &tank[i].a, &tank[i].w); norm(tank[i].a); } scanf("%d", &k); low = 0; high = 3e6; while (low < high - eps) { mid = (low + high) * 0.5; if (check(mid)) high = mid; else low = mid + eps; } printf("%.20f\n", (low + high) * 0.5); return 0; }
12
CPP
#include <bits/stdc++.h> using namespace std; typedef long long ll; int n, m, k; struct Point { double x, y; Point() {} Point(double x, double y) : x(x), y(y) {} double abs() const { return hypot(x, y); } double arg() const { return atan2(y, x); } Point operator*(double o) const { return Point(x * o, y * o); } Point operator+(const Point& o) const { return Point(x + o.x, y + o.y); } Point operator-(const Point& o) const { return Point(x - o.x, y - o.y); } bool operator<(const Point& o) const { return x < o.x - 1e-6 || (x < o.x + 1e-6 && y < o.y - 1e-6); } }; Point A, B; map<double, int> js; int main() { scanf("%lf%lf%lf%lf", &A.x, &A.y, &B.x, &B.y); scanf("%d", &n); double x, y, a, w; for (int i = 0; i < n; i++) { scanf("%lf%lf%lf%lf", &x, &y, &a, &w); if (abs(w) < 1e-6) continue; Point p = {x, y}; double v = -2; for (int j = 0; j <= 100; j++) { Point tp = (B - A) * (j / 100.0) + A; double arg = (tp - p).arg() - a; if (arg > 2 * acos(-1)) arg -= 2 * acos(-1); while (arg < 0) arg += 2 * acos(-1); if (arg > 2 * acos(-1) - arg) arg = 2 * acos(-1) - arg; double t = arg / w; v = v < -1 ? (tp - A).abs() / t : max(v, (tp - A).abs() / t); } js[v]++; } scanf("%d", &k); int sk = 0; double t = -10; for (map<double, int>::reverse_iterator it = js.rbegin(); it != js.rend(); it++) { if (sk + it->second > k) { t = it->first; break; } sk += it->second; } printf("%.4lf\n", t < -1 ? 0 : t); return 0; }
12
CPP
#include <bits/stdc++.h> using namespace std; struct point { double x, y; }; double dist(point &P, point &Q) { double dx = P.x - Q.x, dy = P.y - Q.y; return sqrt(dx * dx + dy * dy); } double inprod(point &O, point &P, point &Q) { return (P.x - O.x) * (Q.x - O.x) + (P.y - O.y) * (Q.y - O.y); } point foot(point &P, point &A, point &B) { double t = inprod(A, B, P) / inprod(A, B, B); point ans = {A.x + (B.x - A.x) * t, A.y + (B.y - A.y) * t}; return ans; } point rotate(point &P, double theta) { double x = P.x * cos(theta) - P.y * sin(theta); double y = P.x * sin(theta) + P.y * cos(theta); point ans = {x, y}; return ans; } double arg(point &P) { double ans = atan2(P.y, P.x); if (ans < 0.0) ans += 2.0 * acos(-1.0); return ans; } bool check(point &A, point &C, double v, double w) { double t = dist(A, C) / v; double theta = arg(C); theta = min(theta, 2.0 * acos(-1.0) - theta); return (w * t > theta); } double func(point A, point B, point P, double a, double w) { int iter; if (w < 1.0E-6) return 0.0; A.x -= P.x; A.y -= P.y; B.x -= P.x; B.y -= P.y; A = rotate(A, -a); B = rotate(B, -a); if (arg(A) > arg(B)) { A.y = -A.y; B.y = -B.y; } point O = {0.0, 0.0}; double low = 0.0, high = 1.0E+7, mid; for ((iter) = 0; (iter) < (int)(100); (iter)++) { mid = (high + low) / 2.0; if (check(A, B, mid, w)) { low = mid; continue; } point H = foot(O, A, B); double h = dist(O, H); if (h * w < mid) { double t = sqrt(mid / (h * w) - 1.0); point C = {H.x + H.y * t, H.y - H.x * t}; if (inprod(C, A, B) < 0.0 && check(A, C, mid, w)) { low = mid; continue; } point D = {H.x - H.y * t, H.y + H.x * t}; if (inprod(D, A, B) < 0.0 && check(A, D, mid, w)) { low = mid; continue; } } high = mid; } return mid; } int main(void) { int N, K, i; point A, B, P; double a, w; cin >> A.x >> A.y >> B.x >> B.y; cin >> N; vector<double> v; for ((i) = 0; (i) < (int)(N); (i)++) { cin >> P.x >> P.y >> a >> w; double tmp = func(A, B, P, a, w); v.push_back(tmp); } cin >> K; v.push_back(0.0); sort(v.begin(), v.end()); double ans = v[N - K]; printf("%.6f\n", ans); return 0; }
12
CPP
#include <bits/stdc++.h> using namespace std; const int MAXN = 10000; int n; double x1, y3223789, x2, y2; double x[MAXN], y[MAXN], w[MAXN], a0[MAXN]; double t1[MAXN], t2[MAXN], d[MAXN], p[MAXN]; double pi = 2 * acos(0.0); double cross(double x1, double y3223789, double x2, double y2) { return x1 * y2 - x2 * y3223789; } double getmin(double x) { while (x < 0) x += 2 * pi; while (x > 2 * pi) x -= 2 * pi; return min(x, 2 * pi - x); } double get_time(int i, double xx, double yy) { double ang = atan2(yy - y[i], xx - x[i]) - a0[i]; return getmin(ang) / w[i]; } double inter(double x1, double y3223789, double x2, double y2, double x3, double y3, double x4, double y4) { double d = cross(x2 - x1, y2 - y3223789, x3 - x4, y3 - y4); double p = cross(x2 - x1, y2 - y3223789, x3 - x1, y3 - y3223789); return p / d; } double nt; int check(int i, double v) { if (get_time(i, x2, y2) < nt) return 1; if (p[i] < 0) return 0; double l = 0, r = min(1.0, p[i]); for (int o = 0; o < 40; o++) { double c1 = (l * 2 + r) / 3; double c2 = (l + 2 * r) / 3; double cx1 = x1 * (1 - c1) + x2 * c1; double cy1 = y3223789 * (1 - c1) + y2 * c1; double cx2 = x1 * (1 - c2) + x2 * c2; double cy2 = y3223789 * (1 - c2) + y2 * c2; double t1 = get_time(i, cx1, cy1); double t2 = get_time(i, cx2, cy2); t1 -= nt * c1; t2 -= nt * c2; if (t1 < 0 || t2 < 0) return 1; if (t1 < t2) r = c2; else l = c1; } return 0; } int getans(double v) { int ans = 0; nt = sqrt((x2 - x1) * (x2 - x1) + (y2 - y3223789) * (y2 - y3223789)) / v; for (int i = 0; i < n; i++) if (check(i, v)) ans++; return ans; } int main() { cin >> x1 >> y3223789 >> x2 >> y2; double dist = sqrt((x2 - x1) * (x2 - x1) + (y2 - y3223789) * (y2 - y3223789)); cin >> n; for (int i = 0; i < n; i++) { cin >> x[i] >> y[i] >> a0[i] >> w[i]; double tmp = cross(x[i] - x1, y[i] - y3223789, x[i] - x2, y[i] - y2); t1[i] = get_time(i, x1, y3223789); t2[i] = get_time(i, x2, y2); p[i] = inter(x[i], y[i], x[i] + y2 - y3223789, y[i] + x1 - x2, x1, y3223789, x2, y2); d[i] = abs(tmp) / dist; } int k; cin >> k; double tt[MAXN]; for (int i = 0; i < n; i++) { double l = 0, r = 4000000; while (r - l > 1e-5) { double v = (l + r) / 2; nt = sqrt((x2 - x1) * (x2 - x1) + (y2 - y3223789) * (y2 - y3223789)) / v; if (check(i, v)) l = v; else r = v; } tt[i] = (l + r) / 2; } sort(tt, tt + n); reverse(tt, tt + n); if (k == n) printf("%.4lf", 0.0); else printf("%.4lf", tt[k]); cout << endl; return 0; }
12
CPP
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:60777216") using namespace std; double xa, ya, xb, yb; int n; double x[11111], y[11111], a[11111], w[11111]; int k; const double pi = acos(-1.); double go(double a, double w) { double v = 0; int num = 100; for (int i(0); i < (num + 1); ++i) { double x = xa + (xb - xa) * i / double(num); double y = ya + (yb - ya) * i / double(num); double q = atan2(y, x) - a; if (q > 2 * pi) q -= 2 * pi; while (q < 0) q += 2 * pi; if (q > 2 * pi - q) q = 2 * pi - q; double T = (w < 1e-5 ? (i == 0 ? 0 : 1e10) : q / w); v = max(v, hypot(x - xa, y - ya) / T); } return v; } int main() { cin >> xa >> ya >> xb >> yb >> n; for (int i(0); i < (n); ++i) scanf("%lf %lf %lf %lf", x + i, y + i, a + i, w + i); cin >> k; vector<double> v; for (int i(0); i < (n); ++i) { xa -= x[i], ya -= y[i], xb -= x[i], yb -= y[i]; v.push_back(go(a[i], w[i])); xa += x[i], ya += y[i], xb += x[i], yb += y[i]; } sort((v).begin(), (v).end()); reverse(v.begin(), v.end()); if (k >= v.size()) puts("0.0000"); else printf("%.4lf\n", v[k]); return 0; }
12
CPP
#include <bits/stdc++.h> using namespace std; const int MAXN = 10086; const double EPS = 1e-6; const double PI = atan2(0.0, -1.0); struct Point { double x, y; Point() {} Point(double x, double y) : x(x), y(y) {} double abs() const { return hypot(x, y); } double arg() const { return atan2(y, x); } }; Point operator+(const Point& lhs, const Point& rhs) { return Point(lhs.x + rhs.x, lhs.y + rhs.y); } Point operator-(const Point& lhs, const Point& rhs) { return Point(lhs.x - rhs.x, lhs.y - rhs.y); } Point operator*(const double& lhs, const Point& rhs) { return Point(lhs * rhs.x, lhs * rhs.y); } double operator*(const Point& lhs, const Point& rhs) { return lhs.x * rhs.x + lhs.y * rhs.y; } double operator%(const Point& lhs, const Point& rhs) { return lhs.x * rhs.y - rhs.x * lhs.y; } double off(double a, double b) { a -= b; while (a < -PI) { a += 2 * PI; } while (a >= PI) { a -= 2 * PI; } return a; } double dis(double a, double b) { return fabs(off(a, b)); } double gao(const Point& a, const Point& b, Point p, double q, double w) { double d = (b - a).abs(); double h = fabs((p - a) % (b - a)) / d; if ((p - a) % (b - a) > 0) { p = p + 2 * h / d * Point(a.y - b.y, b.x - a.x); q = (b - a).arg() * 2 - q; } double q0 = (b - a).arg() - PI / 2; double qa = off((a - p).arg(), q0); double qb = off((b - p).arg(), q0); double qq = off(q, q0); double l = d / (dis(qq, qb) / w), r = 1e8; while (r - l > EPS) { double m = (l + r) / 2; double qc = h * w >= m ? qb : min(qb, max(qa, -acos(sqrt(h * w / m)))); if (dis(qq, qc) / w <= (tan(qc) - tan(qa)) * h / m) { l = m; } else { r = m; } } return (l + r) / 2; } int main() { int n, m; Point a, b, p; double q, w; vector<double> v; scanf("%lf%lf%lf%lf", &a.x, &a.y, &b.x, &b.y); scanf("%d", &n); for (int i = 0; i < n; ++i) { scanf("%lf%lf%lf%lf", &p.x, &p.y, &q, &w); v.push_back(gao(a, b, p, q, w)); } v.push_back(0); sort(v.begin(), v.end()); scanf("%d", &m); printf("%.10lf\n", v[n - m]); return 0; }
12
CPP
#include <bits/stdc++.h> using namespace std; const double pi = acos(-1.00), eps = 1e-9; struct point { double x, y, w, a; }; point st, en, tank[11111]; int n, k; void norm(double &theta) { while (theta < -pi + eps) theta += 2 * pi; while (theta > pi + eps) theta -= 2 * pi; } bool check(double v) { int i, j, s, p, q, nk = 0, it; double now_t, theta, dist, T, w; double ltheta, low, high, mid, mt, vs, fs, bs, mx, my, fx, fy; bool ft; dist = sqrt((en.x - st.x) * (en.x - st.x) + (en.y - st.y) * (en.y - st.y)); T = dist / v; for (i = 0; i < n; i++) { theta = atan2(en.y - tank[i].y, en.x - tank[i].x); while (theta < tank[i].a - eps) theta += 2 * pi; theta -= tank[i].a; if (theta > pi) theta = 2 * pi - theta; now_t = theta / tank[i].w; if (v * now_t < sqrt((en.x - st.x) * (en.x - st.x) + (en.y - st.y) * (en.y - st.y))) { nk++; continue; } ltheta = atan2(st.y - tank[i].y, st.x - tank[i].x); if (tank[i].w == 0) continue; else { while (ltheta < tank[i].a - eps) ltheta += 2 * pi; ltheta -= tank[i].a; w = tank[i].w; if (ltheta > pi) { w = -w; ltheta = 2 * pi - ltheta; } theta = atan2(en.y - st.y, en.x - st.x); low = ltheta / tank[i].w; high = T; if (low > high) continue; mt = low; fs = (st.x + v * cos(theta) * mt - tank[i].x) * sin(tank[i].a + w * mt) - (st.y + v * sin(theta) * mt - tank[i].y) * cos(tank[i].a + w * mt); mt = high; bs = (st.x + v * cos(theta) * mt - tank[i].x) * sin(tank[i].a + w * mt) - (st.y + v * sin(theta) * mt - tank[i].y) * cos(tank[i].a + w * mt); if ((fs < eps && bs > -eps) || (fs > -eps && bs < eps)) { nk++; continue; } for (it = 0; it < 100; it++) { mx = mt = (2 * low + high) / 3.; fx = (st.x + v * cos(theta) * mt - tank[i].x) * sin(tank[i].a + w * mt) - (st.y + v * sin(theta) * mt - tank[i].y) * cos(tank[i].a + w * mt); my = mt = (low + 2 * high) / 3.; fy = (st.x + v * cos(theta) * mt - tank[i].x) * sin(tank[i].a + w * mt) - (st.y + v * sin(theta) * mt - tank[i].y) * cos(tank[i].a + w * mt); if ((fs > 0 && fx > fy) || (fs < 0 && fx < fy)) low = mx; else high = my; } mt = 0.5 * (low + high); fx = (st.x + v * cos(theta) * mt - tank[i].x) * sin(tank[i].a + w * mt) - (st.y + v * sin(theta) * mt - tank[i].y) * cos(tank[i].a + w * mt); if ((fs > 0 && fx < eps) || (fs < 0 && fx > -eps)) { nk++; continue; } } } return nk <= k; } int main() { int i, j, s, p, q; double low, high, mid; scanf("%lf%lf%lf%lf", &st.x, &st.y, &en.x, &en.y); scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%lf%lf%lf%lf", &tank[i].x, &tank[i].y, &tank[i].a, &tank[i].w); norm(tank[i].a); } scanf("%d", &k); low = 0; high = 1e9; while (low < high - eps) { mid = (low + high) * 0.5; if (check(mid)) high = mid; else low = mid + eps; } printf("%.20f\n", (low + high) * 0.5); return 0; }
12
CPP
#include <bits/stdc++.h> using namespace std; template <class T> T abs(T x) { return x > 0 ? x : -x; } const double pi = 2 * asin(1.0); int n; int m; double x[10000], y[10000], a[10000], w[10000]; double ax, ay, bx, by; double dist(double x1, double y8687969, double x2, double y2) { return sqrt( abs(((x2 - x1) * (x2 - x1)) + ((y2 - y8687969) * (y2 - y8687969)))); } double get(int i, double x, double y) { double b = atan2(y - ::y[i], x - ::x[i]); double ang = abs(a[i] - b); if (ang > 2 * pi) ang -= 2 * pi; return min(ang, 2 * pi - ang) / w[i]; } double cross(double x1, double y8687969, double x2, double y2, double x3, double y3, double x4, double y4) { double a = x2 - x1; double b = x3 - x4; double c = x3 - x1; double d = y2 - y8687969; double e = y3 - y4; double f = y3 - y8687969; return (c * e - b * f) / (a * e - b * d); } int calc(double v) { int tmp = 0; double d = dist(ax, ay, bx, by); double dx = (bx - ax); double dy = (by - ay); double t = d / v; for (int i = 0; i < n; i++) { if (w[i] < 1e-6) continue; if (get(i, bx, by) < t) tmp++; else { double l = 0, r = max(0.0, min(1.0, cross(ax, ay, bx, by, x[i], y[i], x[i] + (by - ay), y[i] + (ax - bx)))); for (int it = 0; it < 40; it++) { double s1 = (2 * l + r) / 3; double s2 = (l + 2 * r) / 3; double d1 = get(i, ax + dx * s1, ay + dy * s1) - t * s1; double d2 = get(i, ax + dx * s2, ay + dy * s2) - t * s2; if (d1 < d2) r = s2; else l = s1; } double ss = (l + r) / 2; double dd = get(i, ax + dx * ss, ay + dy * ss) - t * ss; if (dd < 0) tmp++; } } return tmp; } int main() { cin >> ax >> ay >> bx >> by >> n; for (int i = 0; i < n; i++) cin >> x[i] >> y[i] >> a[i] >> w[i]; cin >> m; double l = 0, r = 1e10; for (int it = 0; it < 60; it++) { double s = (l + r) / 2; if (calc(s) <= m) r = s; else l = s; } printf("%.10f\n", r); return 0; }
12
CPP
#include <bits/stdc++.h> using namespace std; pair<double, double> operator-(pair<double, double> a, pair<double, double> b) { return pair<double, double>(a.first - b.first, a.second - b.second); } pair<double, double> st, dir; double _w, _s, _len; inline double f(double t) { double first = st.first + dir.first * t, second = st.second + dir.second * t; double r = atan2(second, first) - _s; while (r < 0) { r += 2 * M_PI; } while (r > 2 * M_PI) { r -= 2 * M_PI; } return _len * t * _w / min(r, 2 * M_PI - r); } double get(pair<double, double> a, pair<double, double> b, double s, double w) { st = a; dir = b - a; _s = s; _w = w; _len = hypot(dir.first, dir.second); double res = 0; for (int i = 0; i <= 100; i++) { double t = f(i / 100.); if (t > res) { res = t; } } return res; } void read(pair<double, double> &p) { double first, second; scanf("%lf%lf", &first, &second); p.first = first; p.second = second; } int main(void) { pair<double, double> a, b; read(a); read(b); int n; scanf("%d", &n); vector<double> v(1, 0); for (int i = 0; i < n; i++) { pair<double, double> p; read(p); double s, w; scanf("%lf%lf", &s, &w); v.push_back(get(a - p, b - p, s, w)); } sort(v.begin(), v.end()); reverse(v.begin(), v.end()); int k; scanf("%d", &k); printf("%.20lf\n", v[k]); return 0; }
12
CPP
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:60777216") using namespace std; double xa, ya, xb, yb; int n; double x[11111], y[11111], a[11111], w[11111]; int k; const double pi = acos(-1.); double go(double a, double w) { double v = 0; int num = 1000; for (int i(0); i < (num + 1); ++i) { double x = xa + (xb - xa) * i / double(num); double y = ya + (yb - ya) * i / double(num); double q = atan2(y, x) - a; if (q > 2 * pi) q -= 2 * pi; while (q < 0) q += 2 * pi; if (q > 2 * pi - q) q = 2 * pi - q; double T = (w < 1e-5 ? (i == 0 ? 0 : 1e10) : q / w); v = max(v, hypot(x - xa, y - ya) / T); } return v; } int main() { cin >> xa >> ya >> xb >> yb >> n; for (int i(0); i < (n); ++i) scanf("%lf %lf %lf %lf", x + i, y + i, a + i, w + i); cin >> k; vector<double> v; for (int i(0); i < (n); ++i) { xa -= x[i], ya -= y[i], xb -= x[i], yb -= y[i]; v.push_back(go(a[i], w[i])); xa += x[i], ya += y[i], xb += x[i], yb += y[i]; } sort((v).begin(), (v).end()); reverse(v.begin(), v.end()); if (k >= v.size()) puts("0.0000"); else printf("%.4lf\n", v[k]); return 0; }
12
CPP
#include <bits/stdc++.h> using namespace std; const double pi2 = acos(-1.0) * 2; double g(double x, double y, double a, double v) { double c = atan2(y, x) - a; for (; c >= pi2; c -= pi2) ; for (; c < 0; c += pi2) ; return min(c, pi2 - c) / v; } double f(double x0, double y0, double x1, double y1, double a, double v) { double dx = x1 - x0, dy = y1 - y0, d = sqrt(dx * dx + dy * dy); double res = 1 / g(x1, y1, a, v); for (double t = 1e-4; t < 1; t += 2e-4) res = max(res, t / g(x0 + dx * t, y0 + dy * t, a, v)); double L = 1e-4, R = 1, l, r, fl, fr; for (int k(0); k < 256; ++k) { l = (L * 2 + R) / 3; r = (L + R * 2) / 3; fl = l / g(x0 + dx * l, y0 + dy * l, a, v); fr = r / g(x0 + dx * r, y0 + dy * r, a, v); if (fl < fr) { res = max(res, fr); L = l; } else { res = max(res, fl); R = r; } } return d * res; } int main() { double Ax, Ay, Bx, By; int n; cin >> Ax >> Ay >> Bx >> By >> n; vector<double> v; for (int i(0); i < n; ++i) { double x, y, a, w; scanf("%lf%lf%lf%lf", &x, &y, &a, &w); v.push_back(f(Ax - x, Ay - y, Bx - x, By - y, a, w)); } sort(v.rbegin(), v.rend()); v.push_back(0); cin >> n; printf("%.12lf\n", v[n]); return 0; }
12
CPP
#include <bits/stdc++.h> using namespace std; const double pi = acos(-1.00), eps = 1e-9; struct point { double x, y, w, a; }; point st, en, tank[11111]; int n, k; void norm(double &theta) { while (theta < -pi + eps) theta += 2 * pi; while (theta > pi + eps) theta -= 2 * pi; } bool check(double v) { int i, j, s, p, q, nk = 0, it; double now_t, theta, dist, T, w; double ltheta, low, high, mid, mt, vs, fs, bs, mx, my, fx, fy; bool ft; dist = sqrt((en.x - st.x) * (en.x - st.x) + (en.y - st.y) * (en.y - st.y)); T = dist / v; for (i = 0; i < n; i++) { theta = atan2(en.y - tank[i].y, en.x - tank[i].x); while (theta < tank[i].a - eps) theta += 2 * pi; theta -= tank[i].a; if (theta > pi) theta = 2 * pi - theta; now_t = theta / tank[i].w; if (v * now_t < sqrt((en.x - st.x) * (en.x - st.x) + (en.y - st.y) * (en.y - st.y))) { nk++; continue; } ltheta = atan2(st.y - tank[i].y, st.x - tank[i].x); theta = ltheta; while (theta < tank[i].a - eps) theta += 2 * pi; theta -= tank[i].a; if (theta > pi) theta = 2 * pi - theta; now_t = theta / tank[i].w; if (v * now_t > sqrt((en.x - st.x) * (en.x - st.x) + (en.y - st.y) * (en.y - st.y))) continue; if (tank[i].w == 0) continue; else { while (ltheta < tank[i].a - eps) ltheta += 2 * pi; ltheta -= tank[i].a; w = tank[i].w; if (ltheta > pi) { w = -w; ltheta = 2 * pi - ltheta; } theta = atan2(en.y - st.y, en.x - st.x); low = ltheta / tank[i].w; high = T; if (low > high) continue; mt = low; fs = (st.x + v * cos(theta) * mt - tank[i].x) * sin(tank[i].a + w * mt) - (st.y + v * sin(theta) * mt - tank[i].y) * cos(tank[i].a + w * mt); mt = high; bs = (st.x + v * cos(theta) * mt - tank[i].x) * sin(tank[i].a + w * mt) - (st.y + v * sin(theta) * mt - tank[i].y) * cos(tank[i].a + w * mt); if ((fs < eps && bs > -eps) || (fs > -eps && bs < eps)) { nk++; continue; } for (it = 0; it < 100; it++) { mx = mt = (2 * low + high) / 3.; fx = (st.x + v * cos(theta) * mt - tank[i].x) * sin(tank[i].a + w * mt) - (st.y + v * sin(theta) * mt - tank[i].y) * cos(tank[i].a + w * mt); my = mt = (low + 2 * high) / 3.; fy = (st.x + v * cos(theta) * mt - tank[i].x) * sin(tank[i].a + w * mt) - (st.y + v * sin(theta) * mt - tank[i].y) * cos(tank[i].a + w * mt); if ((fs > 0 && fx > fy) || (fs < 0 && fx < fy)) low = mx; else high = my; } mt = 0.5 * (low + high); fx = (st.x + v * cos(theta) * mt - tank[i].x) * sin(tank[i].a + w * mt) - (st.y + v * sin(theta) * mt - tank[i].y) * cos(tank[i].a + w * mt); if ((fs > 0 && fx < eps) || (fs < 0 && fx > -eps)) { nk++; continue; } } } if (v == 8) printf("nk=%d\n", nk); return nk <= k; } int main() { int i, j, s, p, q; double low, high, mid; scanf("%lf%lf%lf%lf", &st.x, &st.y, &en.x, &en.y); scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%lf%lf%lf%lf", &tank[i].x, &tank[i].y, &tank[i].a, &tank[i].w); norm(tank[i].a); } scanf("%d", &k); low = 0; high = 1e9; while (low < high - eps) { mid = (low + high) * 0.5; if (check(mid)) high = mid; else low = mid + eps; } printf("%.20f\n", (low + high) * 0.5); return 0; }
12
CPP
#include <bits/stdc++.h> using namespace std; double prodvec(complex<double> p1, complex<double> p2) { return imag(conj(p1) * p2); } double prodesc(complex<double> p1, complex<double> p2) { return real(conj(p1) * p2); } const int limite = 20000; double error = 1e-7; double computa(complex<double> p, complex<double> direccion, double w, double b) { complex<double> pb(b, 0); double tb = abs(arg((pb - p) / direccion)) / w; double sol = b / tb; double x = real(p); double y = imag(p); if (x <= 0) return sol; double inf = sol; double sup = 1000000000; while (inf + error < sup) { double med = (inf + sup) / 2.0; if (med * y / w < y * y) { sup = med; } else { complex<double> corte(x - sqrt(med * y / w - y * y), 0.0); if (real(corte) < 0 or real(corte) > b) sup = med; else { double t = abs(arg((corte - p) / direccion)) / w; double tt = real(corte) / med; if (tt < t) sup = med; else inf = med; } } } return inf; } complex<double> pa, pb; int n; complex<double> p[100000]; complex<double> vp[100000]; double w[100000]; int k; complex<double> leer() { double x, y; cin >> x >> y; return complex<double>(x, y); } int main() { cout.setf(ios::fixed); cout.precision(10); pa = leer(); pb = leer(); complex<double> divi = pb - pa; divi = divi / abs(divi); cin >> n; vector<double> v; for (int i = 0; i < n; i++) { p[i] = leer(); double a; cin >> a; vp[i] = polar(1.0, a); cin >> w[i]; p[i] -= pa; p[i] = p[i] / divi; vp[i] = vp[i] / divi; if (imag(p[i]) < 0) { p[i] = conj(p[i]); vp[i] = conj(vp[i]); } v.push_back(computa(p[i], vp[i], w[i], abs(pb - pa))); } cin >> k; sort(v.begin(), v.end()); if (k >= int(v.size())) cout << 0.0 << endl; else cout << v[int(v.size()) - 1 - k] << endl; }
12
CPP
#include <bits/stdc++.h> using namespace std; const double pi = acos(-1.00), eps = 1e-9; struct point { double x, y, w, a; }; point st, en, tank[11111]; int n, k; void norm(double &theta) { while (theta < -pi + eps) theta += 2 * pi; while (theta > pi + eps) theta -= 2 * pi; } bool check(double v) { int i, j, s, p, q, nk = 0, it; double now_t, theta, dist, T, w; double ltheta, low, high, mid, mt, vs, fs, bs, mx, my, fx, fy; bool ft; dist = sqrt((en.x - st.x) * (en.x - st.x) + (en.y - st.y) * (en.y - st.y)); T = dist / v; for (i = 0; i < n; i++) { theta = atan2(en.y - tank[i].y, en.x - tank[i].x); while (theta < tank[i].a - eps) theta += 2 * pi; theta -= tank[i].a; if (theta > pi) theta = 2 * pi - theta; now_t = theta / tank[i].w; if (v * now_t < sqrt((en.x - st.x) * (en.x - st.x) + (en.y - st.y) * (en.y - st.y))) { nk++; continue; } ltheta = atan2(st.y - tank[i].y, st.x - tank[i].x); theta = ltheta; while (theta < tank[i].a - eps) theta += 2 * pi; theta -= tank[i].a; if (theta > pi) theta = 2 * pi - theta; now_t = theta / tank[i].w; if (v * now_t > sqrt((en.x - st.x) * (en.x - st.x) + (en.y - st.y) * (en.y - st.y))) continue; if (tank[i].w == 0) continue; else { while (ltheta < tank[i].a - eps) ltheta += 2 * pi; ltheta -= tank[i].a; w = tank[i].w; if (ltheta > pi) { w = -w; ltheta = 2 * pi - ltheta; } theta = atan2(en.y - st.y, en.x - st.x); low = ltheta / tank[i].w; high = T; if (low > high) continue; mt = low; fs = (st.x + v * cos(theta) * mt - tank[i].x) * sin(tank[i].a + w * mt) - (st.y + v * sin(theta) * mt - tank[i].y) * cos(tank[i].a + w * mt); mt = high; bs = (st.x + v * cos(theta) * mt - tank[i].x) * sin(tank[i].a + w * mt) - (st.y + v * sin(theta) * mt - tank[i].y) * cos(tank[i].a + w * mt); if ((fs < eps && bs > -eps) || (fs > -eps && bs < eps)) { nk++; continue; } for (it = 0; it < 100; it++) { mx = mt = (2 * low + high) / 3.; fx = (st.x + v * cos(theta) * mt - tank[i].x) * sin(tank[i].a + w * mt) - (st.y + v * sin(theta) * mt - tank[i].y) * cos(tank[i].a + w * mt); my = mt = (low + 2 * high) / 3.; fy = (st.x + v * cos(theta) * mt - tank[i].x) * sin(tank[i].a + w * mt) - (st.y + v * sin(theta) * mt - tank[i].y) * cos(tank[i].a + w * mt); if ((fs > 0 && fx > fy) || (fs < 0 && fx < fy)) low = mx; else high = my; } mt = 0.5 * (low + high); fx = (st.x + v * cos(theta) * mt - tank[i].x) * sin(tank[i].a + w * mt) - (st.y + v * sin(theta) * mt - tank[i].y) * cos(tank[i].a + w * mt); if ((fs > 0 && fx < eps) || (fs < 0 && fx > -eps)) { nk++; continue; } } } return nk <= k; } int main() { int i, j, s, p, q; double low, high, mid; scanf("%lf%lf%lf%lf", &st.x, &st.y, &en.x, &en.y); scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%lf%lf%lf%lf", &tank[i].x, &tank[i].y, &tank[i].a, &tank[i].w); norm(tank[i].a); } scanf("%d", &k); low = 0; high = 1e9; while (low < high - eps) { mid = (low + high) * 0.5; if (check(mid)) high = mid; else low = mid + eps; } printf("%.20f\n", (low + high) * 0.5); return 0; }
12
CPP
#include <bits/stdc++.h> using namespace std; double cal_ang(double x, double y) { double res = acos(abs(x / hypot(x, y))); if (x <= 0 && y >= 0) return acos(-1) - res; if (x <= 0 && y < 0) return acos(-1) + res; if (x > 0 && y < 0) return acos(-1) * 2.0 - res; return res; } int main() { double x0, x1, y0, y1, t0, t1, t2, t3; int n, k; vector<double> time; scanf("%lf%lf%lf%lf%d", &x0, &y0, &x1, &y1, &n); for (long long i = 0; i < ((long long)n); i++) { scanf("%lf%lf%lf%lf", &t0, &t1, &t2, &t3); if (t3 < 1e-5) continue; x0 -= t0; x1 -= t0; y0 -= t1; y1 -= t1; double tmp = 0; for (long long j = 0; j < ((long long)100 + 1); j++) { double x = x0 + (x1 - x0) * j / (double)100; double y = y0 + (y1 - y0) * j / (double)100; double ang = cal_ang(x, y); ang -= t2; if (ang < 0) ang += 2.0 * acos(-1); while (ang > acos(-1) * 2.0) ang -= acos(-1) * 2.0; ang = min(acos(-1) * 2.0 - ang, ang); tmp = max(tmp, hypot(x0 - x, y0 - y) / (ang / t3)); } time.push_back(tmp); x0 += t0; x1 += t0; y0 += t1; y1 += t1; } scanf("%d", &k); if (((long long)time.size()) <= k) { printf("0.0000\n"); return 0; } sort(time.begin(), time.end()); reverse(time.begin(), time.end()); printf("%.4lf\n", time[k]); }
12
CPP
#include <bits/stdc++.h> using namespace std; const double pi = acos(-1); struct pnt { double x, y; pnt(double x = 0, double y = 0) : x(x), y(y) {} pnt operator+(pnt a) { return pnt(x + a.x, y + a.y); } pnt operator-(pnt a) { return pnt(x - a.x, y - a.y); } pnt operator/(double a) { return pnt(x / a, y / a); } pnt operator*(double a) { return pnt(x * a, y * a); } double operator^(pnt a) { return x * a.y - y * a.x; } double operator&(pnt a) { return x * a.x + y * a.y; } double dist() { return sqrt(x * x + y * y); } void input() { scanf("%lf %lf", &x, &y); } void print() { printf("%lf %lf\n", x, y); } } pos[100010], A, B; double vec[100010], a[100010], w[100010]; int n; double angle(pnt b, pnt a, pnt c) { return acos((b - a & c - a) / (c - a).dist() / (b - a).dist()); } double D; int P; double f(double x, int i, double d, double t, double dl, double v, double al) { return dl + v * x - d * sin(w[i] * x) / sin(al + w[i] * x); } bool check(double v) { int tot = 0; for (int i = 0; i < n; i++) { if (tot > P) return 0; pnt tp = pnt(cos(a[i]), sin(a[i])); double t = angle(pos[i] + tp, pos[i], B) / w[i]; if (v * t < D) { tot++; continue; } t = angle(pos[i] + tp, pos[i], A) / w[i]; if (v * t > D - 1e-7) continue; double al = angle(pos[i], A, B); if (al > pi / 2 - 1e-7) continue; double d = (pos[i] - A).dist(); double T = (D - v * t) / v, dl = v * t; double st = 0, en = T; while (st < en - 1e-7) { double x = (2 * st + en) / 3, y = (st + 2 * en) / 3; if (f(x, i, d, t, dl, v, al) < f(y, i, d, t, dl, v, al)) en = y; else st = x; } if (f(st, i, d, t, dl, v, al) < 1e-7) tot++; } if (tot > P) return 0; return 1; } int main() { A.input(), B.input(); D = (A - B).dist(); scanf("%d", &n); for (int i = 0; i < n; i++) { pos[i].input(); cin >> a[i] >> w[i]; } scanf("%d", &P); double l = 0, r = 1e9; while (l < r - 1e-7) { double mid = (l + r) / 2; if (check(mid)) r = mid; else l = mid; } printf("%.12lf\n", l); }
12
CPP
#include <bits/stdc++.h> using namespace std; int n, m, k; bool debug = false; struct Point { double x, y; Point() {} Point(double x, double y) : x(x), y(y) {} double abs() const { return hypot(x, y); } double arg() const { return atan2(y, x); } Point operator*(double o) const { return Point(x * o, y * o); } Point operator+(const Point& o) const { return Point(x + o.x, y + o.y); } Point operator-(const Point& o) const { return Point(x - o.x, y - o.y); } bool operator<(const Point& o) const { return x < o.x - 1e-6 || (x < o.x + 1e-6 && y < o.y - 1e-6); } }; Point A, B; map<double, int> js; int main() { scanf("%lf%lf%lf%lf", &A.x, &A.y, &B.x, &B.y); scanf("%d", &n); double x, y, a, w; for (int i = 0; i < n; i++) { scanf("%lf%lf%lf%lf", &x, &y, &a, &w); if (abs(w) < 1e-6) continue; Point p = {x, y}; double v = -2; for (int j = 0; j <= 100; j++) { Point tp = (B - A) * (j / 100.0) + A; double arg = (tp - p).arg() - a; if (arg > 2 * acos(-1)) arg -= 2 * acos(-1); while (arg < 0) arg += 2 * acos(-1); if (arg > 2 * acos(-1) - arg) arg = 2 * acos(-1) - arg; double t = arg / w; v = v < -1 ? (tp - A).abs() / t : max(v, (tp - A).abs() / t); } js[v]++; } scanf("%d", &k); int sk = 0; double t = -10; for (map<double, int>::reverse_iterator it = js.rbegin(); it != js.rend(); it++) { if (sk + it->second > k) { t = it->first; break; } sk += it->second; } printf("%.4lf\n", t < -1 ? 0 : t); return 0; }
12
CPP
#include <bits/stdc++.h> int main(int argc, char** argv) { double x1, y1, x2, y2, x3, y3, x5, y5, x1tab[10000], y1tab[10000], x5tab[10000], y5tab[10000], angle[10000], w[10000]; int k, n; double length; double miny[10001]; scanf("%lf%lf%lf%lf", &x2, &y2, &x3, &y3); length = sqrt(x2 * x2 - 2 * x2 * x3 + x3 * x3 + y2 * y2 - 2 * y2 * y3 + y3 * y3); scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%lf%lf%lf%lf", &x5tab[i], &y5tab[i], &angle[i], &w[i]); x1tab[i] = cos(angle[i]); y1tab[i] = sin(angle[i]); } scanf("%d", &k); for (int i = 0; i < n; i++) { x1 = x1tab[i]; y1 = y1tab[i]; x5 = x5tab[i]; y5 = y5tab[i]; double a, b, c, d, e; a = x1 * (x3 - x2) / sqrt(x2 * x2 - 2 * x2 * x3 + x3 * x3 + y2 * y2 - 2 * y2 * y3 + y3 * y3) + y1 * (y3 - y2) / sqrt(x2 * x2 - 2 * x2 * x3 + x3 * x3 + y2 * y2 - 2 * y2 * y3 + y3 * y3); b = x1 * (x2 - x5) + y1 * (y2 - y5); c = (x3 - x2) * (x3 - x2) / (x2 * x2 - 2 * x2 * x3 + x3 * x3 + y2 * y2 - 2 * y2 * y3 + y3 * y3) + (y3 - y2) * (y3 - y2) / (x2 * x2 - 2 * x2 * x3 + x3 * x3 + y2 * y2 - 2 * y2 * y3 + y3 * y3); d = (2 * (x2 - x5)) * (x3 - x2) / sqrt(x2 * x2 - 2 * x2 * x3 + x3 * x3 + y2 * y2 - 2 * y2 * y3 + y3 * y3) + (2 * (y2 - y5)) * (y3 - y2) / sqrt(x2 * x2 - 2 * x2 * x3 + x3 * x3 + y2 * y2 - 2 * y2 * y3 + y3 * y3); e = (x2 - x5) * (x2 - x5) + (y2 - y5) * (y2 - y5); double min1 = acos((a * length + b) / sqrt(c * length * length + d * length + e)) / w[i] / length; for (int j = 1; j < 100; j++) { double x = length * j / 100; double min2 = acos((a * x + b) / sqrt(c * x * x + d * x + e)) / w[i] / x; if (min2 < min1) min1 = min2; } miny[i] = 1 / min1; } if (k >= n) { printf("%.4f", 0); } else { std::sort(miny, miny + n); printf("%.4f", miny[n - k - 1]); } }
12
CPP
#include <bits/stdc++.h> using namespace std; int n, m, k; struct Point { double x, y; Point() {} Point(double x, double y) : x(x), y(y) {} double abs() const { return hypot(x, y); } double arg() const { return atan2(y, x); } Point operator*(double o) const { return Point(x * o, y * o); } Point operator+(const Point& o) const { return Point(x + o.x, y + o.y); } Point operator-(const Point& o) const { return Point(x - o.x, y - o.y); } bool operator<(const Point& o) const { return x < o.x - 1e-6 || (x < o.x + 1e-6 && y < o.y - 1e-6); } }; Point A, B; map<double, int> js; int main() { scanf("%lf%lf%lf%lf", &A.x, &A.y, &B.x, &B.y); scanf("%d", &n); double x, y, a, w; for (int i = 0; i < n; i++) { scanf("%lf%lf%lf%lf", &x, &y, &a, &w); if (abs(w) < 1e-6) continue; Point p = {x, y}; double v = -2; for (int j = 0; j <= 100; j++) { Point tp = (B - A) * (j / 100.0) + A; double arg = (tp - p).arg() - a; if (arg > 2 * acos(-1)) arg -= 2 * acos(-1); while (arg < 0) arg += 2 * acos(-1); if (arg > 2 * acos(-1) - arg) arg = 2 * acos(-1) - arg; double t = arg / w; v = v < -1 ? (tp - A).abs() / t : max(v, (tp - A).abs() / t); } js[v]++; } scanf("%d", &k); int sk = 0; double t = -10; for (map<double, int>::reverse_iterator it = js.rbegin(); it != js.rend(); it++) { if (sk + it->second > k) { t = it->first; break; } sk += it->second; } printf("%.4lf\n", t < -1 ? 0 : t); return 0; }
12
CPP
#include <bits/stdc++.h> using namespace std; template <class _T> inline _T sqr(const _T& x) { return x * x; } template <class _T> inline string tostr(const _T& a) { ostringstream os(""); os << a; return os.str(); } const double PI = 3.1415926535897932384626433832795; const double EPS = 1e-11; struct tp { double x, y; }; int n, k; tp a[10240]; double ang[10240]; double w[10240]; tp p1, p2; double b0[10240]; double tt; inline double dist(tp p1, tp p2) { return sqrt(sqr(p1.x - p2.x) + sqr(p1.y - p2.y)); } inline bool hit(double s, int i) { if (w[i] < EPS) return false; double da = fabs(ang[i] - b0[i]); da = min(da, 2.0 * PI - da); double t = da / w[i]; if (t <= tt) return true; int z = 2; for (int j = 0; j < (int)(z); j++) { double mi = j * 1.0 / z; double ma = (j + 1.0) / z; for (int tt = 0; tt < (int)(101); tt++) { if (ma - mi < 1e-11) break; double q1 = (mi * 2.0 + ma) / 3.0; double q2 = (mi + 2.0 * ma) / 3.0; tp pp1, pp2; pp1.x = p1.x + (p2.x - p1.x) * q1; pp1.y = p1.y + (p2.y - p1.y) * q1; pp2.x = p1.x + (p2.x - p1.x) * q2; pp2.y = p1.y + (p2.y - p1.y) * q2; double aa1 = atan2(pp1.y - a[i].y, pp1.x - a[i].x); double aa2 = atan2(pp2.y - a[i].y, pp2.x - a[i].x); double a1 = fabs(ang[i] - aa1); a1 = min(a1, 2.0 * PI - a1); double a2 = fabs(ang[i] - aa2); a2 = min(a2, 2.0 * PI - a2); double t1 = a1 / w[i]; double t2 = a2 / w[i]; double d1 = dist(p1, pp1); double d2 = dist(p1, pp2); t1 -= d1 / s; t2 -= d2 / s; if (t1 <= 0.0) return true; if (t2 <= 0.0) return true; if (t1 < t2) ma = q2; else mi = q1; } } return false; } bool check(double s) { int cnt = 0; tt = dist(p1, p2) / s; for (int i = 0; i < (int)(n); i++) { if (cnt + n - i <= k) return true; cnt += hit(s, i); if (cnt > k) return false; } return cnt <= k; } int main() { cout << setiosflags(ios::fixed) << setprecision(10); int x, y; scanf("%d%d", &x, &y); p1.x = x; p1.y = y; scanf("%d%d", &x, &y); p2.x = x; p2.y = y; scanf("%d", &n); for (int i = 0; i < (int)(n); i++) { scanf("%d%d", &x, &y); a[i].x = x; a[i].y = y; double aa, ww; scanf("%lf%lf", &aa, &ww); if (aa > PI) { aa -= 2.0 * PI; } ang[i] = aa; w[i] = ww; } scanf("%d", &k); for (int i = 0; i < (int)(n); i++) { b0[i] = atan2(p2.y - a[i].y, p2.x - a[i].x); } double mi = 3e-5; double ma = 4e+6; while (ma - mi > 3e-5 && (ma - mi) / (ma + mi) > 3e-5) { double q = (mi + ma) * 0.5; *(check(q) ? &ma : &mi) = q; } printf("%0.5lf\n", (double)mi); return 0; }
12
CPP
#include <bits/stdc++.h> using namespace std; const double EPS = 1e-9, INF = 1e15, PI = acos(-1.0); double sq(double x) { return x * x; } struct P { double x, y; P() {} P(double x, double y) : x(x), y(y) {} void eat() { scanf("%lf%lf", &x, &y); } P operator+(const P &p) const { return P(x + p.x, y + p.y); } P operator-(const P &p) const { return P(x - p.x, y - p.y); } P operator*(double k) { return P(x * k, y * k); } double operator*(P p) { return x * p.x + y * p.y; } double operator^(P p) { return x * p.y - y * p.x; } double mag2() { return x * x + y * y; } double mag() { return sqrt(mag2()); } bool operator<(const P &p) const { if ((fabs((x) - (p.x)) < EPS)) return ((y) < (p.y) - EPS); return ((x) < (p.x) - EPS); } bool operator==(const P &p) const { return (fabs((x) - (p.x)) < EPS) && (fabs((y) - (p.y)) < EPS); } P rot() { return P(-y, x); } P rot(double the) { return P(x * cos(the) - y * sin(the), x * sin(the) + y * cos(the)); } P nor() { if ((fabs((mag()) - (0)) < EPS)) return *this; return *this * (1. / mag()); } P ref(P n) { n = n.nor(); return n * (n * (*this)) * 2 - *this; } void out() { printf("(%f,%f)\n", x, y); } }; bool ssi(P a, P b, P c, P d, P &res) { P cd = d - c, ab = b - a; if ((fabs((cd ^ ab) - (0)) < EPS)) return 0; double t = ((a ^ ab) - (c ^ ab)) / (cd ^ ab); double s = ((c ^ cd) - (a ^ cd)) / (ab ^ cd); res = c + cd * t; return 1; } bool Ccw(P a, P b, P c) { return ((0) < ((b - a) ^ (c - a)) - EPS); } bool Btw(P a, P b, P c) { return (fabs(((a - c).mag()) - ((b - a).mag() + (c - b).mag())) < EPS); } double Ang(P a, P b) { double res = atan2(a ^ b, a * b); if (((res) < (0) - EPS)) res += 2 * PI; return res; } const int N = 20360; P p[N], dir[N]; P a, b; double the[N], spd[N], low[N]; int n, k; int main() { a.eat(); b.eat(); scanf("%d", &n); for (int i = 0; i < n; i++) { p[i].eat(); scanf("%lf%lf", the + i, spd + i); dir[i] = P(cos(the[i]), sin(the[i])); } scanf("%d", &k); double len = (a - b).mag(); for (int i = 0; i < n; i++) { low[i] = 0; if (((0) < (spd[i]) - EPS)) for (int j = 0; j < 2036 + 1; j++) { P c = a + (b - a) * (j / 2036.0); double ang = Ang(c - p[i], dir[i]); ang = min(ang, 2 * PI - ang); low[i] = max(low[i], len * (j / 2036.0) / (ang / spd[i])); } } sort(low, low + n); double ans = k == n ? 0 : low[n - 1 - k]; printf("%.20f\n", ans); return 0; }
12
CPP
#include <bits/stdc++.h> int main(int argc, char** argv) { double x1, y1, x2, y2, x3, y3, x5, y5, x1tab[10000], y1tab[10000], x5tab[10000], y5tab[10000], angle[10000], w[10000]; int k, n; double length; double miny[10001]; scanf("%lf%lf%lf%lf", &x2, &y2, &x3, &y3); length = sqrt(x2 * x2 - 2 * x2 * x3 + x3 * x3 + y2 * y2 - 2 * y2 * y3 + y3 * y3); scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%lf%lf%lf%lf", &x5tab[i], &y5tab[i], &angle[i], &w[i]); x1tab[i] = cos(angle[i]); y1tab[i] = sin(angle[i]); } scanf("%d", &k); for (int i = 0; i < n; i++) { x1 = x1tab[i]; y1 = y1tab[i]; x5 = x5tab[i]; y5 = y5tab[i]; double a, b, c, d, e; a = x1 * (x3 - x2) / sqrt(x2 * x2 - 2 * x2 * x3 + x3 * x3 + y2 * y2 - 2 * y2 * y3 + y3 * y3) + y1 * (y3 - y2) / sqrt(x2 * x2 - 2 * x2 * x3 + x3 * x3 + y2 * y2 - 2 * y2 * y3 + y3 * y3); b = x1 * (x2 - x5) + y1 * (y2 - y5); c = (x3 - x2) * (x3 - x2) / (x2 * x2 - 2 * x2 * x3 + x3 * x3 + y2 * y2 - 2 * y2 * y3 + y3 * y3) + (y3 - y2) * (y3 - y2) / (x2 * x2 - 2 * x2 * x3 + x3 * x3 + y2 * y2 - 2 * y2 * y3 + y3 * y3); d = (2 * (x2 - x5)) * (x3 - x2) / sqrt(x2 * x2 - 2 * x2 * x3 + x3 * x3 + y2 * y2 - 2 * y2 * y3 + y3 * y3) + (2 * (y2 - y5)) * (y3 - y2) / sqrt(x2 * x2 - 2 * x2 * x3 + x3 * x3 + y2 * y2 - 2 * y2 * y3 + y3 * y3); e = (x2 - x5) * (x2 - x5) + (y2 - y5) * (y2 - y5); double min1 = acos((a * length + b) / sqrt(c * length * length + d * length + e)) / w[i] / length; for (int j = 1; j < 1000; j++) { double x = length * j / 1000; double min2 = acos((a * x + b) / sqrt(c * x * x + d * x + e)) / w[i] / x; if (min2 < min1) min1 = min2; } miny[i] = 1 / min1; } if (k >= n) { printf("%.4f", 0); } else { std::sort(miny, miny + n); printf("%.4f", miny[n - k - 1]); } }
12
CPP
#include <bits/stdc++.h> using namespace std; double cal_ang(double x, double y) { double res = acos(abs(x / hypot(x, y))); if (x <= 0 && y >= 0) return acos(-1) - res; if (x <= 0 && y < 0) return acos(-1) + res; if (x > 0 && y < 0) return acos(-1) * 2.0 - res; return res; } int main() { double x0, x1, y0, y1, t0, t1, t2, t3; int n, k; vector<double> time; scanf("%lf%lf%lf%lf%d", &x0, &y0, &x1, &y1, &n); for (long long i = 0; i < ((long long)n); i++) { scanf("%lf%lf%lf%lf", &t0, &t1, &t2, &t3); if (t3 < 1e-5) continue; x0 -= t0; x1 -= t0; y0 -= t1; y1 -= t1; double tmp = 0; for (long long j = 0; j < ((long long)100 + 1); j++) { double x = x0 + (x1 - x0) * j / (double)100; double y = y0 + (y1 - y0) * j / (double)100; double ang = cal_ang(x, y); ang -= t2; if (ang < 0) ang += 2.0 * acos(-1); while (ang > acos(-1) * 2.0) ang -= acos(-1) * 2.0; ang = min(acos(-1) * 2.0 - ang, ang); tmp = max(tmp, hypot(x0 - x, y0 - y) / (ang / t3)); } time.push_back(tmp); x0 += t0; x1 += t0; y0 += t1; y1 += t1; } scanf("%d", &k); if (((long long)time.size()) <= k) { printf("0.0000\n"); return 0; } sort(time.begin(), time.end()); reverse(time.begin(), time.end()); printf("%.4lf\n", time[k]); return 0; }
12
CPP
#include <bits/stdc++.h> using namespace std; pair<double, double> operator-(pair<double, double> a, pair<double, double> b) { return pair<double, double>(a.first - b.first, a.second - b.second); } pair<double, double> st, dir; double _w, _s, _len; inline double f(double t) { double first = st.first + dir.first * t, second = st.second + dir.second * t; double r = atan2(second, first) - _s; while (r < 0) { r += 2 * M_PI; } while (r > 2 * M_PI) { r -= 2 * M_PI; } return _len * t * _w / min(r, 2 * M_PI - r); } double get(pair<double, double> a, pair<double, double> b, double s, double w) { st = a; dir = b - a; _s = s; _w = w; _len = hypot(dir.first, dir.second); double res = 0; for (int i = 0; i <= 200; i++) { double t = f(i / 200.); if (t > res) { res = t; } } return res; } void read(pair<double, double> &p) { double first, second; scanf("%lf%lf", &first, &second); p.first = first; p.second = second; } int main(void) { pair<double, double> a, b; read(a); read(b); int n; scanf("%d", &n); vector<double> v(1, 0); for (int i = 0; i < n; i++) { pair<double, double> p; read(p); double s, w; scanf("%lf%lf", &s, &w); v.push_back(get(a - p, b - p, s, w)); } sort(v.begin(), v.end()); reverse(v.begin(), v.end()); int k; scanf("%d", &k); printf("%.20lf\n", v[k]); return 0; }
12
CPP
#include <bits/stdc++.h> using namespace std; double cal_ang(double x, double y) { double res = acos(abs(x / hypot(x, y))); if (x <= 0 && y >= 0) return acos(-1) - res; if (x <= 0 && y < 0) return acos(-1) + res; if (x > 0 && y < 0) return acos(-1) * 2.0 - res; return res; } int main() { double x0, x1, y0, y1, t0, t1, t2, t3; int n, k; vector<double> time; scanf("%lf%lf%lf%lf%d", &x0, &y0, &x1, &y1, &n); for (long long i = 0; i < ((long long)n); i++) { scanf("%lf%lf%lf%lf", &t0, &t1, &t2, &t3); if (t3 < 1e-5) continue; x0 -= t0; x1 -= t0; y0 -= t1; y1 -= t1; double tmp = 0; for (long long j = 0; j < ((long long)100 + 1); j++) { double x = x0 + (x1 - x0) * j / (double)100; double y = y0 + (y1 - y0) * j / (double)100; double ang = cal_ang(x, y); ang -= t2; if (ang < 0) ang += 2.0 * acos(-1); while (ang > acos(-1) * 2.0) ang -= acos(-1) * 2.0; ang = min(acos(-1) * 2.0 - ang, ang); tmp = max(tmp, hypot(x0 - x, y0 - y) / (ang / t3)); } time.push_back(tmp); x0 += t0; x1 += t0; y0 += t1; y1 += t1; } scanf("%d", &k); if (((long long)time.size()) <= k) { printf("0.0000\n"); return 0; } sort(time.begin(), time.end()); reverse(time.begin(), time.end()); printf("%.4lf\n", time[k]); }
12
CPP
#include <bits/stdc++.h> using namespace std; double cal_ang(double x, double y) { double res = acos(abs(x / hypot(x, y))); if (x <= 0 && y >= 0) return acos(-1) - res; if (x <= 0 && y < 0) return acos(-1) + res; if (x > 0 && y < 0) return acos(-1) * 2.0 - res; return res; } int main() { double x0, x1, y0, y1, t0, t1, t2, t3; int n, k; vector<double> time; scanf("%lf%lf%lf%lf%d", &x0, &y0, &x1, &y1, &n); for (long long i = 0; i < ((long long)n); i++) { scanf("%lf%lf%lf%lf", &t0, &t1, &t2, &t3); if (t3 < 1e-5) continue; x0 -= t0; x1 -= t0; y0 -= t1; y1 -= t1; double tmp = 0; for (long long j = 0; j < ((long long)100 + 1); j++) { double x = x0 + (x1 - x0) * j / (double)100; double y = y0 + (y1 - y0) * j / (double)100; double ang = cal_ang(x, y); ang -= t2; if (ang < 0) ang += 2.0 * acos(-1); while (ang > acos(-1) * 2.0) ang -= acos(-1) * 2.0; ang = min(acos(-1) * 2.0 - ang, ang); tmp = max(tmp, hypot(x0 - x, y0 - y) / (ang / t3)); } time.push_back(tmp); x0 += t0; x1 += t0; y0 += t1; y1 += t1; } scanf("%d", &k); if (((long long)time.size()) <= k) { printf("0.0000\n"); return 0; } sort(time.begin(), time.end()); reverse(time.begin(), time.end()); printf("%.4lf\n", time[k]); }
12
CPP
#include <bits/stdc++.h> using namespace std; const int MOD = (int)1e9 + 7; const int INF = (int)1e9; const long long LINF = (long long)1e18; const long double PI = acos((long double)-1); const long double EPS = 1e-9; inline long long gcd(long long a, long long b) { long long r; while (b) { r = a % b; a = b; b = r; } return a; } inline long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } inline long long fpow(long long n, long long k, int p = MOD) { long long r = 1; for (; k; k >>= 1) { if (k & 1) r = r * n % p; n = n * n % p; } return r; } template <class T> inline int chkmin(T& a, const T& val) { return val < a ? a = val, 1 : 0; } template <class T> inline int chkmax(T& a, const T& val) { return a < val ? a = val, 1 : 0; } template <class T> inline T isqrt(T k) { T r = sqrt(k) + 1; while (r * r > k) r--; return r; } template <class T> inline T icbrt(T k) { T r = cbrt(k) + 1; while (r * r * r > k) r--; return r; } inline void addmod(int& a, int val, int p = MOD) { if ((a = (a + val)) >= p) a -= p; } inline void submod(int& a, int val, int p = MOD) { if ((a = (a - val)) < 0) a += p; } inline int mult(int a, int b, int p = MOD) { return (long long)a * b % p; } inline int inv(int a, int p = MOD) { return fpow(a, p - 2, p); } inline int sign(long double x) { return x < -EPS ? -1 : x > +EPS; } inline int sign(long double x, long double y) { return sign(x - y); } const int maxn = 1000000 + 5; const int mod = (int)1e8 + 7; int n; vector<int> adj[maxn]; map<pair<int, int>, int> hs; int f[maxn]; int g[maxn]; int cnt[maxn]; int tot; inline int query(pair<int, int> val) { if (hs.count(val)) return hs[val]; int res = int((hs).size()); return hs[val] = res; } void add(int val) { if (!(cnt[val]++)) { tot++; } } void rem(int val) { if (!(--cnt[val])) { tot--; } } void dfs(int u, int p = -1) { pair<int, int> r; for (int i = (0); i < (int((adj[u]).size())); i++) { int v = adj[u][i]; if (v != p) { dfs(v, u); addmod(r.first, fpow(3, f[v])); addmod(r.second, fpow(5, f[v], mod), mod); } } add(f[u] = query(r)); } void dfs2(int u, int p = -1, int pv = -1) { pair<int, int> r; if (pv != -1) { addmod(r.first, fpow(3, pv)); addmod(r.second, fpow(5, pv, mod), mod); add(pv); } for (int i = (0); i < (int((adj[u]).size())); i++) { int v = adj[u][i]; if (v != p) { addmod(r.first, fpow(3, f[v])); addmod(r.second, fpow(5, f[v], mod), mod); } } rem(f[u]); g[u] = tot; for (int i = (0); i < (int((adj[u]).size())); i++) { int v = adj[u][i]; if (v != p) { submod(r.first, fpow(3, f[v])); submod(r.second, fpow(5, f[v], mod), mod); dfs2(v, u, query(r)); addmod(r.first, fpow(3, f[v])); addmod(r.second, fpow(5, f[v], mod), mod); } } if (pv != -1) { rem(pv); } add(f[u]); } void solve() { cin >> n; for (int i = (0); i < (n - 1); i++) { int u, v; cin >> u >> v; u--, v--; adj[u].push_back(v), adj[v].push_back(u); } dfs(0), dfs2(0); pair<int, int> best; for (int i = (0); i < (n); i++) chkmax(best, make_pair(g[i], i)); cout << best.second + 1 << "\n"; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); solve(); return 0; }
10
CPP
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:512000000") using namespace std; void solve(bool); void precalc(); clock_t start; int testNumber = 1; bool todo = true; int main() { start = clock(); int t = 1; cout.sync_with_stdio(0); cin.tie(0); precalc(); cout.precision(10); cout << fixed; int testNum = 1; while (t--) { solve(true); ++testNumber; } return 0; } template <typename T> T binpow(T q, T w, T mod) { if (!w) return 1 % mod; if (w & 1) return q * 1LL * binpow(q, w - 1, mod) % mod; return binpow(q * 1LL * q % mod, w / 2, mod); } template <typename T> T gcd(T q, T w) { while (w) { q %= w; swap(q, w); } return q; } template <typename T> T lcm(T q, T w) { return q / gcd(q, w) * w; } void precalc() {} const long long C = 100500; long long pr[2] = {234211, 23423541}; long long mods[2] = {1000000007, 1000000009}; long long powers[2][C]; class Treap { public: typedef struct _node { long long key; long long cnt; long long prior; long long val[2]; _node* l; _node* r; _node(long long key) : key(key), l(nullptr), r(nullptr), cnt(1) { prior = (rand() << 16) | rand(); val[0] = (key + 1); val[1] = (key + 1); } void push() {} void recalc() { cnt = 1 + Cnt(l) + Cnt(r); for (long long w = 0; w < 2; ++w) { val[w] = powers[w][Cnt(r)] * (key + 1) % mods[w]; if (l) { val[w] += powers[w][Cnt(r) + 1] * l->val[w]; } if (r) { val[w] += r->val[w]; } val[w] %= mods[w]; } } long long get_hash() { return (val[0] * 1LL << 32) | val[1]; } static long long Cnt(_node* v) { if (!v) return 0; return v->cnt; } } * node; static long long Cnt(node v) { if (!v) return 0; return v->cnt; } node root; size_t Size; node merge(node l, node r) { if (!l) return r; if (!r) return l; if (l->prior < r->prior) { l->push(); l->r = merge(l->r, r); l->recalc(); return l; } else { r->push(); r->l = merge(l, r->l); r->recalc(); return r; } } void split(node v, long long key, node& l, node& r) { l = r = nullptr; if (!v) return; v->push(); if (v->key < key) { l = v; split(l->r, key, l->r, r); l->recalc(); } else { r = v; split(r->l, key, l, r->l); r->recalc(); } } void splitCnt(node v, long long key, node& l, node& r) { l = r = nullptr; if (!v) return; v->push(); long long lef = Cnt(v->l) + 1; if (key >= lef) { l = v; splitCnt(l->r, key - lef, l->r, r); l->recalc(); } else { r = v; splitCnt(r->l, key, l, r->l); r->recalc(); } } public: Treap() { root = nullptr; Size = 0; } size_t size() const { return Size; } void insert(long long key) { node l = nullptr, r = nullptr; split(root, key, l, r); ++Size; root = merge(merge(l, new _node(key)), r); } void erase(long long key) { node l, m, r; split(root, key, l, m); splitCnt(m, 1, m, r); root = merge(l, r); --Size; } long long get_hash() { if (!root) { return 0; } return root->get_hash(); } }; vector<vector<long long>> g; long long best_ans = 0; long long best_v = -1; map<long long, long long> trees; map<long long, long long> cnt; vector<long long> down; vector<long long> subtree; long long dfs1(long long v, long long p) { subtree[v] = 1; Treap children; for (long long to : g[v]) { if (to == p) { continue; } long long child = dfs1(to, v); children.insert(child); subtree[v] += subtree[to]; } long long cur = children.get_hash() ^ subtree[v]; if (!trees.count(cur)) { long long id = (long long)trees.size(); trees[cur] = id; } cur = trees[cur]; ++cnt[cur]; down[v] = cur; return cur; } vector<vector<long long>> hashes; void dec(long long cur) { --cnt[cur]; if (cnt[cur] == 0) { cnt.erase(cur); } } void inc(long long cur) { ++cnt[cur]; } long long n; void dfs2(long long v, long long p, long long up) { long long cand = (long long)cnt.size() + 1; if (best_ans < cand || best_ans == cand && v < best_v) { best_ans = cand; best_v = v; } Treap cur; if (up != -1) { cur.insert(up); } for (long long to : g[v]) { if (to == p) { continue; } cur.insert(down[to]); } for (long long to : g[v]) { if (to == p) { continue; } dec(down[to]); cur.erase(down[to]); long long now = cur.get_hash() ^ (n - subtree[to]); long long id; if (!trees.count(now)) { id = (long long)trees.size(); trees[now] = id; } else { id = trees[now]; } inc(id); dfs2(to, v, id); cur.insert(down[to]); inc(down[to]); dec(id); } } void solve(bool read) { for (long long w = 0; w < 2; ++w) { powers[w][0] = 1; for (long long i = 1; i < C; ++i) { powers[w][i] = powers[w][i - 1] * pr[w] % mods[w]; } } cin >> n; g.resize(n); for (long long i = 1; i < n; ++i) { long long a, b; cin >> a >> b; --a; --b; g[a].push_back(b); g[b].push_back(a); } down.resize(n); subtree.resize(n); dfs1(0, 0); hashes.resize(n); dec(down[0]); dfs2(0, 0, -1); cout << best_v + 1 << "\n"; }
10
CPP
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; int n, head[N], cnt, base1, base2, base3, dfn[N], idx, idfn[N], sz[N], fa[N]; int d[N], a[N]; unsigned long long hx[N]; struct edge { int to, nxt; } e[N << 1]; struct data { unsigned long long val; int sz, l, r; inline bool operator<(const data& rhs) const { return val == rhs.val ? (sz == rhs.sz ? l < rhs.l : sz < rhs.sz) : val < rhs.val; } }; vector<data> vec; void dfs(int now) { hx[now] = 0; idfn[dfn[now] = ++idx] = now; sz[now] = 1; for (int i = head[now]; i; i = e[i].nxt) if (!dfn[e[i].to]) { fa[e[i].to] = now; dfs(e[i].to); sz[now] += sz[e[i].to]; hx[now] ^= hx[e[i].to] * base3 + base1; } hx[now] ^= (unsigned long long)sz[now] * base2 + 1; int l = dfn[now], r = dfn[now] + sz[now]; vec.push_back((data){hx[now], sz[now], 1, l - 1}); vec.push_back((data){hx[now], sz[now], r, n}); } void dfs2(int now) { unsigned long long vx = hx[now], nx = hx[now] ^ ((unsigned long long)sz[now] * base2 + 1); for (int i = head[now]; i; i = e[i].nxt) if (dfn[now] < dfn[e[i].to]) { hx[now] = nx ^ (hx[e[i].to] * base3 + base1); if (fa[now]) hx[now] ^= hx[fa[now]] * base3 + base1; hx[now] ^= (unsigned long long)(n - sz[e[i].to]) * base2 + 1; vec.push_back((data){hx[now], n - sz[e[i].to], dfn[e[i].to], dfn[e[i].to] + sz[e[i].to] - 1}); dfs2(e[i].to); } hx[now] = vx; } int main() { srand(time(0)); base1 = rand() % 20000 + 10000, base2 = rand() % 30000 + 23333, base3 = rand() % 1919810 + 114514; scanf("%d", &n); for (int i = 1; i < n; ++i) { int u, v; scanf("%d%d", &u, &v); e[++cnt] = (edge){v, head[u]}, head[u] = cnt; e[++cnt] = (edge){u, head[v]}, head[v] = cnt; } idx = 0; vec.clear(); dfs(1); dfs2(1); sort(vec.begin(), vec.end()); data nw = vec[0]; for (int i = 1; i <= vec.size(); ++i) if (i == vec.size() || nw.val != vec[i].val || nw.sz != vec[i].sz || nw.r < vec[i].l) { ++d[nw.l], --d[nw.r + 1]; nw = vec[i]; } else nw.r = vec[i].r; for (int i = 1; i <= n; ++i) d[i] += d[i - 1]; for (int i = 1; i <= n; ++i) a[i] = max(a[i], d[i]), d[i] = 0; printf("%d\n", idfn[max_element(a + 1, a + n + 1) - a]); return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; const int N = 100100; const long long MOD = 100030001, H = 47; map<int, long long> d[N]; vector<int> adj[N]; set<pair<long long, int> > s; vector<pair<int, pair<long long, int> > > nei[N]; long long r[N], l[N], num[N], ch[N], h[3 * N], n, sz, ans, o = 10, c = 11; void add(int ind, int val) { auto it = s.lower_bound({ind, val}); if (it == s.end() || it->first != ind) s.insert({ind, val}); else { if (it->second + val != 0) s.insert({ind, it->second + val}); s.erase(it); } return; } void f(int v, int u) { add(d[v][v], -1); add(d[u][u], 1); add(d[v][u], 1); add(d[u][v], -1); return; } void DFS1(int v, int par = 0) { for (int u : adj[v]) { if (u != par) { DFS1(u, v); ch[v] += ch[u]; nei[v].push_back({d[u][v], {ch[u], u}}); } } ch[v]++; long long ans = o; sort(nei[v].begin(), nei[v].end()); for (auto u : nei[v]) { ans = ans * h[2 * u.second.first] % MOD; ans = (ans + u.first) % MOD; } d[v][par] = (ans * H + c) % MOD; return; } void DFS2(int v, int par = 0) { nei[v].clear(); for (int u : adj[v]) { nei[v].push_back({d[u][v], {ch[u], u}}); if (u == par) nei[v].back().second.first = n - ch[v]; } sort(nei[v].begin(), nei[v].end()); l[0] = o, r[nei[v].size() + 1] = c, num[nei[v].size() + 1] = 1; for (int i = 1; i <= nei[v].size(); i++) { auto u = nei[v][i - 1]; l[i] = (l[i - 1] * h[u.second.first * 2] + d[u.second.second][v]) % MOD; } for (int i = nei[v].size(); i > 0; i--) { auto u = nei[v][i - 1]; num[i] = num[i + 1] + u.second.first * 2; r[i] = (r[i + 1] + d[u.second.second][v] * h[num[i + 1]]) % MOD; } for (int i = 0; i < nei[v].size(); i++) { d[v][nei[v][i].second.second] = (l[i] * h[num[i + 2]] + r[i + 2]) % MOD; } for (int u : adj[v]) if (u != par) DFS2(u, v); add(d[v][par], 1); return; } void DFS3(int v, int par = 0) { if (s.size() > sz) { ans = v; sz = s.size(); } for (int u : adj[v]) { if (u != par) { f(v, u); DFS3(u, v); } } f(v, par); return; } int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); int u, v; cin >> n; h[0] = 1; for (int i = 0; i <= n * 2 + 10; i++) h[i + 1] = h[i] * H % MOD; for (int i = 1; i < n; i++) { cin >> u >> v; adj[--u].push_back(--v); adj[v].push_back(u); } if (n == 1) { cout << 1 << endl; return 0; } DFS1(0); DFS2(0); DFS3(0); cout << ans + 1 << endl; return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; const int C = 26; void add(int &x, int y) { x += y; while (x >= MOD) x -= MOD; while (x < 0) x += MOD; } int fix(int x) { while (x >= MOD) x -= MOD; while (x < 0) x += MOD; return x; } int pw(int a, int b) { int ret = 1; while (b) { if (b & 1) ret = 1ll * ret * a % MOD; b >>= 1; a = 1ll * a * a % MOD; } return ret; } int mul(int a, int b) { return (long long)a * b % MOD; } const int MAXN = 1e5 + 10; int n, power[MAXN << 1]; vector<int> g[MAXN]; void plant() { power[0] = 1; for (int i = 1; i <= 2 * n; i++) { power[i] = fix(power[i - 1] + power[i - 1]); } } map<int, array<int, 2>> FUCK[MAXN]; map<array<int, 2>, int> fr; array<int, 2> join(array<int, 2> a, array<int, 2> b) { return {a[0] + b[0], fix(a[1] + mul(power[a[0] << 1], b[1]))}; } array<int, 2> wrap(array<int, 2> a) { return {a[0] + 1, fix(mul(2, a[1]) + 1)}; } void dfs_up(int u, int p) { vector<array<int, 2>> ch; for (int v : g[u]) { if (v != p) { dfs_up(v, u); ch.push_back(wrap(FUCK[v][u])); } } sort(ch.begin(), ch.end()); for (array<int, 2> a : ch) { FUCK[u][p] = join(FUCK[u][p], a); } ++fr[FUCK[u][p]]; } void dfs_down(int u, int p) { vector<array<int, 2>> vec; for (int v : g[u]) { vec.push_back(wrap(FUCK[v][u])); } int deg = g[u].size(); vector<int> sec(deg); iota(sec.begin(), sec.end(), 0); sort(sec.begin(), sec.end(), [&](int i, int j) { return vec[i] < vec[j]; }); vector<array<int, 2>> pre(deg), suf(deg); pre[0] = vec[sec[0]]; for (int i = 1; i < deg; i++) { pre[i] = join(pre[i - 1], vec[sec[i]]); } suf[deg - 1] = vec[sec[deg - 1]]; for (int i = deg - 2; ~i; --i) { suf[i] = join(vec[sec[i]], suf[i + 1]); } for (int i = 0; i < deg; i++) { FUCK[u][g[u][sec[i]]] = join(i ? pre[i - 1] : array<int, 2>(), i + 1 < deg ? suf[i + 1] : array<int, 2>()); } FUCK[u][-1] = pre[deg - 1]; for (int v : g[u]) { if (v != p) { dfs_down(v, u); } } } void remove(array<int, 2> a) { if (--fr[a] == 0) { fr.erase(a); } } array<int, 2> ans; void reroot(int u, int p) { ans = max(ans, {(int)fr.size(), u}); for (int v : g[u]) { if (v != p) { remove(FUCK[u][-1]); remove(FUCK[v][u]); ++fr[FUCK[u][v]]; ++fr[FUCK[v][-1]]; reroot(v, u); ++fr[FUCK[u][-1]]; ++fr[FUCK[v][u]]; remove(FUCK[u][v]); remove(FUCK[v][-1]); } } } void solve() { cin >> n; if (n == 1) { cout << 1 << endl; return; } for (int i = 1; i < n; i++) { int a, b; cin >> a >> b; --a; --b; g[a].push_back(b); g[b].push_back(a); } plant(); dfs_up(0, -1); dfs_down(0, -1); reroot(0, -1); cout << ans[1] + 1 << "\n"; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int te = 1; for (int w = 1; w <= te; w++) { solve(); } return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); uniform_int_distribution<long long> rng_int; long long modpow(long long b, long long e, long long mod) { long long ans = 1; for (; e; b = b * b % mod, e /= 2) if (e & 1) ans = ans * b % mod; return ans; } const int N = 100005; int ans = 0, n, opt = 1; vector<int> g[N]; long long subxor[N]; map<long long, long long> hashset; map<pair<long long, int>, long long> hashkeep; long long first(long long x, int y) { if (y) { if (hashkeep.count({x, y})) return hashkeep[{x, y}]; return hashkeep[{x, y}] = rng_int(rng); } return 0; } map<long long, int> childxor[N]; void reroot(int x, int y) { hashset[subxor[x]]--; hashset[subxor[y]]--; if (hashset[subxor[x]] == 0) hashset.erase(subxor[x]); if (hashset[subxor[y]] == 0) hashset.erase(subxor[y]); subxor[y] ^= first(subxor[x], childxor[y][subxor[x]]) ^ first(subxor[x], childxor[y][subxor[x]] - 1); childxor[y][subxor[x]]--; subxor[x] ^= first(subxor[y], childxor[x][subxor[y]]) ^ first(subxor[y], childxor[x][subxor[y]] + 1); childxor[x][subxor[y]]++; hashset[subxor[x]]++; hashset[subxor[y]]++; } void make(int x, int p = -1) { for (auto w : g[x]) { if (w == p) continue; make(w, x); childxor[x][subxor[w]]++; } for (auto w : childxor[x]) { subxor[x] ^= first(w.first, w.second); } hashset[subxor[x]]++; } void dfs(int x, int p = -1) { if ((int)(hashset).size() > ans) ans = (int)(hashset).size(), opt = x; for (auto w : g[x]) { if (w == p) continue; reroot(w, x); dfs(w, x); reroot(x, w); } } int32_t main() { scanf("%d", &n); for (int i = 0; i < (n - 1); ++i) { int x, y; scanf("%d%d", &x, &y); g[x].push_back(y); g[y].push_back(x); } make(1); dfs(1); printf("%d\n", opt); }
10
CPP
#include <bits/stdc++.h> using namespace std; void print(int a) { printf("%d", a); } void print(pair<int, int> a) { printf("(%d,%d)", a.first, a.second); } template <class T, class U> void print(pair<T, U> p) { printf("("); print(p.first); printf(","); print(p.second); printf(")"); } template <class T> void print(vector<T> v) { printf("{ "); for (T e : v) { print(e); printf(" "); } printf("}"); } int add(int a, int b, int mod) { return ((a += b) >= mod) ? a - mod : a; } void adding(int& a, int b, int mod) { a = add(a, b, mod); } int mul(int a, int b, int mod) { return a * 1ll * b % mod; } int mod[2] = {1000000009, 1000000007}; pair<int, int> operator+(const pair<int, int>& A, const pair<int, int>& B) { return pair<int, int>(add(A.first, B.first, mod[0]), add(A.second, B.second, mod[1])); } pair<int, int> operator*(const pair<int, int>& A, const pair<int, int>& B) { return pair<int, int>(mul(A.first, B.first, mod[0]), mul(A.second, B.second, mod[1])); } const int N = 200005; pair<int, int> pw[N]; pair<int, int> dp[N]; int COUNT[N]; vector<int> g[N]; int n; map<pair<int, int>, int> mp; void dfs(int u, int p) { if (u != p) { g[u].erase(find((g[u]).begin(), (g[u]).end(), p)); } COUNT[u] = 1; dp[u] = pair<int, int>(1, 1); for (int e : g[u]) { dfs(e, u); COUNT[u] += COUNT[e]; } sort((g[u]).begin(), (g[u]).end(), [&](int a, int b) { return dp[a] < dp[b]; }); for (int e : g[u]) { dp[u] = (dp[u] * pw[2 * COUNT[e]]) + dp[e]; } dp[u] = dp[u] + dp[u]; mp[dp[u]]++; } pair<int, int> ans(1, 1); int res[N]; void modify(pair<int, int> hash, int v) { int& first = mp[hash]; first += v; if (first == 0) mp.erase(mp.find(hash)); } pair<int, int> make_tree(pair<int, int> hash, int len) { pair<int, int> h = pw[2 * len] + hash; return h + h; } void play(int u, pair<int, int> phash) { vector<pair<pair<int, int>, int>> hash; for (int i = (0); i <= ((int)(g[u]).size() - 1); ++i) { int e = g[u][i]; hash.emplace_back(dp[e], e); } hash.emplace_back(phash, -1); for (int i = ((int)(hash).size() - 1); i >= (1); --i) { if (hash[i] < hash[i - 1]) swap(hash[i], hash[i - 1]); } int sz = (int)(hash).size(); vector<pair<int, int>> L(sz), R(sz); vector<int> sum(sz); for (int i = (0); i <= (sz - 1); ++i) { L[i] = hash[i].first; if (hash[i].second != -1) sum[i] = COUNT[hash[i].second]; else sum[i] = n - COUNT[u]; if (i > 0) L[i] = (L[i - 1] * pw[2 * sum[i]]) + L[i], sum[i] += sum[i - 1]; } for (int i = (sz - 1); i >= (0); --i) { R[i] = hash[i].first; if (i < (int)(hash).size() - 1) R[i] = (R[i] * pw[2 * (sum.back() - sum[i])]) + R[i + 1]; } pair<int, int> h = make_tree(L.back(), sum.back()); modify(h, 1); ans = max(ans, pair<int, int>((int)(mp).size(), u)); res[u] = (int)(mp).size(); modify(h, -1); for (int i = (0); i <= (sz - 1); ++i) { int e = hash[i].second; if (e == -1) continue; pair<int, int> nw(0, 0); if (i > 0) nw = L[i - 1]; if (i < sz - 1) nw = (nw * pw[2 * (sum.back() - sum[i])]) + R[i + 1]; nw = make_tree(nw, sum.back() - COUNT[e]); modify(nw, 1); modify(dp[e], -1); play(e, nw); modify(dp[e], 1); modify(nw, -1); } } int main() { pw[0] = {1, 1}; for (int i = (1); i <= (200000); ++i) pw[i] = pw[i - 1] + pw[i - 1]; scanf("%d", &n); for (int i = (1); i <= (n - 1); ++i) { int a, b; scanf("%d %d", &a, &b); g[a].emplace_back(b); g[b].emplace_back(a); } dfs(1, 1); int sav = (int)(mp).size(); modify(dp[1], -1); play(1, pair<int, int>(0, 0)); modify(dp[1], 1); printf("%d", ans.second); return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 10; int n, tot, ans, mx, cnt, dif; int head[N], num[N]; unsigned long long val[N], f[N]; map<unsigned long long, int> mp; int read() { int ret = 0; char c = getchar(); while (!isdigit(c)) c = getchar(); while (isdigit(c)) ret = ret * 10 + (c ^ 48), c = getchar(); return ret; } struct Tway { int v, nex; } e[N]; void add(int u, int v) { e[++tot] = (Tway){v, head[u]}; head[u] = tot; e[++tot] = (Tway){u, head[v]}; head[v] = tot; } void ins(int x) { if (!num[x]) ++dif; ++num[x]; } void del(int x) { --num[x]; if (!num[x]) --dif; } int get(int x) { return mp.count(x) ? mp[x] : mp[x] = ++cnt; } void dfs1(int x, int fa) { unsigned long long sum = 0; for (int i = head[x]; i; i = e[i].nex) if (e[i].v ^ fa) dfs1(e[i].v, x), sum += val[f[e[i].v]]; f[x] = get(sum); ins(f[x]); } void dfs2(int x, int fa, int c) { del(f[x]); if (dif + 1 > mx) mx = dif + 1, ans = x; unsigned long long sum = 0; for (int i = head[x]; i; i = e[i].nex) if (e[i].v ^ fa) sum += val[f[e[i].v]]; sum += val[c]; for (int i = head[x]; i; i = e[i].nex) { int v = e[i].v; if (v == fa) continue; unsigned long long snow = sum - val[f[v]]; ins(get(snow)); dfs2(v, x, get(snow)); del(get(snow)); } ins(f[x]); } unsigned long long rnd() { return (unsigned long long)(rand() + 1) * 2333 + (unsigned long long)(rand() + 1) * 19260817 + (unsigned long long)((rand() + 1231) << 28); } int main() { n = read(); for (int i = 1; i <= n * 2; ++i) val[i] = rnd(); for (int i = 1; i < n; ++i) add(read(), read()); dfs1(1, 0); dfs2(1, 0, 0); printf("%d\n", ans); return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; const int S = 3; struct modint { int n; modint() : n(0) {} modint(int n) : n(n) {} }; using modints = array<modint, S>; modints one = {1, 1, 1}; modint operator+(modint a, modint b) { return modint((a.n += b.n) >= mod ? a.n - mod : a.n); } modint operator*(modint a, modint b) { return modint(1LL * a.n * b.n % mod); } bool operator<(modint a, modint b) { return a.n < b.n; } modints operator+(modints a, modints b) { modints ret; for (int i = 0; i < S; i++) { ret[i] = a[i] + b[i]; } return ret; } modints operator*(modints a, modints b) { modints ret; for (int i = 0; i < S; i++) { ret[i] = a[i] * b[i]; } return ret; } const int N = 1e5; int n; vector<int> g[N]; modints rnd[N]; int height[N]; vector<int> h; modints val[N]; map<modints, int> mp; pair<int, int> ans; int cnt = 0; void add(modints a) { if (mp[a]++ == 0) cnt++; } void del(modints a) { if (mp[a]-- == 1) cnt--; } void dfs(int u, int p) { for (int v : g[u]) if (v != p) { dfs(v, u); height[u] = max(height[u], height[v] + 1); } val[u] = one; for (int v : g[u]) if (v != p) { val[u] = val[u] * (rnd[height[u]] + val[v]); } add(val[u]); } void dfs2(int u, int p) { auto tmp = val[u]; del(tmp); int n = g[u].size(); h.clear(); h.push_back(0); for (int v : g[u]) { h.push_back(height[v] + 1); } sort(h.rbegin(), h.rend()); int h0 = h[0]; int h1 = h[1]; modints L0 = one; modints L1 = one; vector<modints> R0(n); vector<modints> R1(n); R0[n - 1] = one; R1[n - 1] = one; for (int i = n - 2; i >= 0; i--) { R0[i] = R0[i + 1] * (rnd[h0] + val[g[u][i + 1]]); R1[i] = R1[i + 1] * (rnd[h1] + val[g[u][i + 1]]); } ans = max(ans, make_pair(cnt, u)); for (int i = 0; i < n; i++) { int v = g[u][i]; if (height[v] + 1 != h0) { height[u] = h0; val[u] = L0 * R0[i]; } else { height[u] = h1; val[u] = L1 * R1[i]; } L0 = L0 * (rnd[h0] + val[v]); L1 = L1 * (rnd[h1] + val[v]); if (v == p) continue; add(val[u]); dfs2(v, u); del(val[u]); } add(tmp); } int main() { mt19937 mt(time(NULL)); uniform_int_distribution<int> uni(0, mod - 1); for (int i = 0; i < N; i++) { for (int j = 0; j < S; j++) { rnd[i][j] = uni(mt); } } cin >> n; for (int i = 0; i < n - 1; i++) { int u, v; scanf("%d %d", &u, &v); u--; v--; g[u].push_back(v); g[v].push_back(u); } if (n == 1) { cout << 1 << endl; return 0; } dfs(0, -1); dfs2(0, -1); cout << ans.second + 1 << endl; }
10
CPP
#include <bits/stdc++.h> using namespace std; const unsigned long long mod = 1e6 + 3; const unsigned long long xr = 279787; const int maxn = 1e5 + 10; vector<int> g[maxn]; unsigned long long up[maxn], dw[maxn]; void dfs1(int u, int f) { dw[u] = 0; for (int i = 0; i < g[u].size(); i++) { int v = g[u][i]; if (v == f) continue; dfs1(v, u); dw[u] += dw[v] * mod; } dw[u] ^= xr; } void dfs2(int u, int f) { unsigned long long tmp = ((dw[u] ^ xr) + up[u] * mod); for (int i = 0; i < g[u].size(); i++) { int v = g[u][i]; if (v == f) continue; up[v] = (tmp - dw[v] * mod) ^ xr; dfs2(v, u); } } unsigned long long rt(int v) { return ((dw[v] ^ xr) + up[v] * mod) ^ xr; } map<unsigned long long, int> mp; int cur = 0; void update(unsigned long long d, unsigned long long a) { --mp[d]; if (mp[d] == 0) --cur; ++mp[a]; if (mp[a] == 1) ++cur; } int ans, ansid; void dfs3(int u, int f) { if (cur > ans) { ans = cur; ansid = u; } for (int i = 0; i < g[u].size(); i++) { int v = g[u][i]; if (v == f) continue; update(dw[v], up[v]); update(rt(u), rt(v)); dfs3(v, u); update(up[v], dw[v]); update(rt(v), rt(u)); } } int main() { int n; scanf("%d", &n); for (int i = 0; i < n - 1; i++) { int u, v; scanf("%d%d", &u, &v); g[u].push_back(v); g[v].push_back(u); } dfs1(1, 0); dfs2(1, 0); ++mp[rt(1)]; for (int i = 2; i <= n; i++) ++mp[dw[i]]; cur = mp.size(); ans = -1; dfs3(1, 0); printf("%d\n", ansid); return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; static int n, ans, pos, sz[100000 + 10]; static vector<int> G[100000 + 10]; static unsigned long long H[100000 + 10], p[100000 + 10], pi[100000 + 10]; static unordered_map<unsigned long long, int> cnt; unsigned long long eval(int x, int fa = 0) { unsigned long long &r = H[x] = 0; sz[x] = 0; for (int v : G[x]) if (v != fa) { r += eval(v, x) * p[sz[v]]; sz[x] += 1; } r = (r + 1) * p[sz[x]]; cnt[r]++; return r; } inline void inc(unsigned long long x) { cnt[x]++; } inline void dec(unsigned long long x) { cnt[x]--; if (!cnt[x]) cnt.erase(x); } void dfs(int x, int fa = 0) { if (cnt.size() > ans) { ans = cnt.size(); pos = x; } unsigned long long cpyx = H[x]; for (int v : G[x]) if (v != fa) { unsigned long long cpyv = H[v]; dec(H[x]); dec(H[v]); H[x] = ((((H[x] * pi[sz[x]]) - 1) - (H[v] * p[sz[v]])) + 1) * p[sz[x] - 1]; H[v] = ((((H[v] * pi[sz[v]]) - 1) + (H[x] * p[sz[x] - 1])) + 1) * p[sz[v] + 1]; sz[x]--; sz[v]++; inc(H[x]); inc(H[v]); dfs(v, x); dec(H[x]); dec(H[v]); H[x] = cpyx; H[v] = cpyv; sz[x]++; sz[v]--; inc(H[x]); inc(H[v]); } } int main() { scanf("%d", &n); p[0] = 1; pi[0] = 1; for (int i = 1; i < n; i++) { int u, v; scanf("%d%d", &u, &v); G[u].push_back(v); G[v].push_back(u); p[i] = p[i - 1] * 19260817ULL; pi[i] = pi[i - 1] * 7089841341079321457ULL; } eval(1); dfs(1); printf("%d\n", pos); fprintf(stderr, "ans = %d\n", ans); return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; int IN() { int c, f, x; while (!isdigit(c = getchar()) && c != '-') ; c == '-' ? (f = 1, x = 0) : (f = 0, x = c - '0'); while (isdigit(c = getchar())) x = (x << 1) + (x << 3) + c - '0'; return !f ? x : -x; } const int N = 1e5 + 19; const int p = 1e9 + 7; struct Edge { int y, nxt; } E[N * 2]; map<pair<int, int>, int> M; map<int, int> C; int las[N]; int n, cnt, mx, res, num; void Link(int x, int y) { E[cnt] = (Edge){y, las[x]}; las[x] = cnt++; E[cnt] = (Edge){x, las[y]}; las[y] = cnt++; } int F(int x, int fa) { if (M.count(make_pair(x, fa))) return M[make_pair(x, fa)]; int res = 1; for (int i = las[x], y; ~i; i = E[i].nxt) if ((y = E[i].y) != fa) { int tmp = F(y, x); res = (res + 1ll * tmp * tmp % p * tmp) % p; } return M[make_pair(x, fa)] = res; } void dfs(int x, int fa) { int tmp = F(x, fa); if (++C[tmp] == 1) num++; if (x != 1) { tmp = (tmp + 1ll * F(fa, x) * F(fa, x) % p * F(fa, x)) % p; } for (int i = las[x], y; ~i; i = E[i].nxt) if ((y = E[i].y) != fa) { int val = (tmp - 1ll * F(y, x) * F(y, x) % p * F(y, x) % p + p) % p; M[make_pair(x, y)] = val; dfs(y, x); } } void Dfs(int x, int fa) { if (num > mx) mx = num, res = x; for (int i = las[x], y; ~i; i = E[i].nxt) if ((y = E[i].y) != fa) { if (++C[F(x, y)] == 1) num++; if (--C[F(y, x)] == 0) num--; Dfs(y, x); if (++C[F(y, x)] == 1) num++; if (--C[F(x, y)] == 0) num--; } } int main() { memset(las, -1, sizeof(las)); n = IN(); for (int i = 1; i < n; i++) Link(IN(), IN()); F(1, -1); dfs(1, -1); Dfs(1, -1); printf("%d\n", res); }
10
CPP
#include <bits/stdc++.h> using namespace std; int n; vector<int> v[100002]; const int h_num = 9; long long int B[h_num] = {1011235817, 1011235817, 1011235817, 987654347, 1011235817, 1021328611, 987654347, 1011235817, 1000000007}; struct HASH { vector<long long int> val; bool emp; HASH(int v_ = 0) { val.assign(h_num, 0); emp = false; for (int i = 0; i < h_num; i++) { val[i] = v_; } } vector<long long int> get() { return val; } }; HASH R[100002]; HASH operator*(const HASH &a, const HASH &b) { if (a.emp) { return b; } if (b.emp) { return a; } HASH r; for (int i = 0; i < h_num; i++) { r.val[i] = a.val[i] * b.val[i]; if (r.val[i] >= B[i]) r.val[i] %= B[i]; } return r; } HASH operator+(const HASH &a, HASH &b) { if (a.emp) { return a; } if (b.emp) { return b; } HASH r; for (int i = 0; i < h_num; i++) { r.val[i] = a.val[i] + b.val[i]; if (r.val[i] >= B[i]) { r.val[i] %= B[i]; } } return r; } HASH h[100002]; map<vector<long long int>, int> tmp; int dep[100002]; inline void dfs(int b, int pr = -1) { vector<HASH> vv; for (int i = 0; i < v[b].size(); i++) { if (pr != v[b][i]) { dfs(v[b][i], b); dep[b] = max(dep[b], dep[v[b][i]] + 1); } } for (int i = 0; i < v[b].size(); i++) { if (pr != v[b][i]) { vv.push_back(h[v[b][i]] + R[dep[b]]); } } if (vv.size() == 0) { h[b] = HASH(1); return; } HASH sum = vv[0]; for (int i = 1; i < vv.size(); i++) { sum = sum * vv[i]; } h[b] = sum; return; } int maxt; int idd; inline void dfs2(int b, int pr, map<vector<long long int>, int> &mp, HASH par2, int par_dep) { par_dep++; mp[h[b].get()]--; if (mp[h[b].get()] == 0) { mp.erase(h[b].get()); } vector<HASH> vv[2]; vector<HASH> im1[2]; vector<HASH> im2[2]; vector<int> H; H.push_back(par_dep); for (int i = 0; i < v[b].size(); i++) { if (v[b][i] != pr) { H.push_back(dep[v[b][i]] + 1); } } sort(H.begin(), H.end(), greater<int>()); if (H.size() == 1) { H.push_back(H.back()); } for (int i = 0; i < v[b].size(); i++) { if (v[b][i] != pr) { for (int j = 0; j < 2; j++) { vv[j].push_back(h[v[b][i]] + R[H[j]]); } } } for (int j = 0; j < 2; j++) { im1[j] = im2[j] = vv[j]; } for (int j = 0; j < 2; j++) { for (int i = 1; i < im1[j].size(); i++) { im1[j][i] = im1[j][i] * im1[j][i - 1]; } for (int i = (int)(im2[j].size()) - 2; i >= 0; i--) { im2[j][i] = im2[j][i] * im2[j][i + 1]; } } HASH par[2]; par[0] = par2 + R[H[0]]; par[1] = par2 + R[H[1]]; HASH cur[2]; cur[0] = par[0]; cur[1] = par[1]; for (int j = 0; j < 2; j++) { if (im1[j].size()) { cur[j] = cur[j] * im1[j].back(); } } mp[cur[0].get()]++; if (maxt < mp.size()) { maxt = mp.size(); idd = b; } if (maxt == mp.size()) { idd = min(idd, b); } mp[cur[0].get()]--; if (mp[cur[0].get()] == 0) { mp.erase(cur[0].get()); } int id = -1; for (int i = 0; i < v[b].size(); i++) { if (v[b][i] == pr) continue; id++; HASH go; go.emp = true; int flag = 0; if (H[0] == dep[v[b][i]] + 1) { flag++; } if (id) { go = go * im1[flag][id - 1]; } if (id + 1 < im2[flag].size()) { go = go * im2[flag][id + 1]; } go = go * par[flag]; if (go.emp == true) { go = HASH(1); } mp[go.get()]++; dfs2(v[b][i], b, mp, go, H[flag]); mp[go.get()]--; if (mp[go.get()] == 0LL) { mp.erase(go.get()); } } mp[h[b].get()]++; } bool use[5000000]; vector<int> pr; int main() { for (int i = 2; i < 5000000; i++) { if (use[i] == false) { for (int j = i * 2; j < 5000000; j += i) { use[j] = true; } pr.push_back(i); } } cin >> n; if (n == 1) { cout << 1 << endl; return 0; } for (int i = 0; i < 100002; i++) { for (int j = 0; j < h_num; j++) { R[i].val[j] = pr[rand() % pr.size()]; } } for (int i = 1; i < n; i++) { int a, b; scanf("%d%d", &a, &b); a--; b--; v[a].push_back(b); v[b].push_back(a); } int root = 0; dfs(root); for (int i = 0; i < n; i++) { tmp[h[i].get()]++; } HASH f; f.emp = true; dfs2(root, -1, tmp, f, -1); cout << idd + 1 << endl; return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; const int N = 1000 * 100 + 10; const int MOD = 1000 * 1000 * 1000 + 7; const int POD = 983; vector<int> Graf[N]; long long SubtreeHash[N]; long long Pot[N]; int Size[N]; map<long long, int> Rozne; int diff; pair<int, int> result; void Wczytaj(int &n) { int i, v, u; scanf("%d", &n); for (i = 0; i < n - 1; i++) { scanf("%d%d", &v, &u); Graf[v].push_back(u); Graf[u].push_back(v); } Pot[0] = 1; for (i = 1; i < N; i++) Pot[i] = (Pot[i - 1] * POD) % MOD; } long long DFSInicjuj(int v, int ojciec) { int i, suma; long long aktualny_hasz; vector<pair<long long, pair<int, int> > > Hasze; Size[v] = 1; Hasze.push_back(make_pair((long long)'(', make_pair(1, -1))); for (i = 0; i < Graf[v].size(); i++) { if (Graf[v][i] != ojciec) { SubtreeHash[Graf[v][i]] = DFSInicjuj(Graf[v][i], v); Hasze.push_back(make_pair(SubtreeHash[Graf[v][i]], make_pair(2 * Size[Graf[v][i]], Graf[v][i]))); Size[v] += Size[Graf[v][i]]; } } Hasze.push_back(make_pair((long long)')', make_pair(1, -1))); sort(Hasze.begin() + 1, Hasze.end() - 1); aktualny_hasz = 0; suma = 0; for (i = 0; i < Hasze.size(); i++) { aktualny_hasz = (aktualny_hasz + Pot[suma] * Hasze[i].first) % MOD; suma += Hasze[i].second.first; } return aktualny_hasz; } void Update(int v, long long nowy, long long stary) { Rozne[stary]--; if (Rozne[stary] == 0) diff--; Rozne[nowy]++; if (Rozne[nowy] == 1) diff++; result = max(result, make_pair(diff, v)); } void DFSPopraw(int v, int ojciec, long long upper_hash, int n) { int i, suma; long long aktualny_hasz, new_upper; vector<int> Suma_Pref; vector<long long> DiffHasze, Hasze_Pref; vector<pair<long long, pair<int, int> > > Hasze; Hasze.push_back(make_pair((long long)'(', make_pair(1, -1))); for (i = 0; i < Graf[v].size(); i++) { if (Graf[v][i] != ojciec) { Hasze.push_back(make_pair(SubtreeHash[Graf[v][i]], make_pair(2 * Size[Graf[v][i]], Graf[v][i]))); DiffHasze.push_back(SubtreeHash[Graf[v][i]]); } } if (upper_hash != -1) { DiffHasze.push_back(upper_hash); Hasze.push_back( make_pair(upper_hash, make_pair(2 * (n - Size[v]), ojciec))); } Hasze.push_back(make_pair((long long)')', make_pair(1, -1))); sort(Hasze.begin() + 1, Hasze.end() - 1); Hasze_Pref.push_back(0); Suma_Pref.push_back(0); suma = 0; for (i = 0; i < Hasze.size(); i++) { Hasze_Pref.push_back((Hasze_Pref.back() + Pot[suma] * Hasze[i].first) % MOD); suma += Hasze[i].second.first; Suma_Pref.push_back(suma); } aktualny_hasz = Hasze.back().first; for (i = Hasze.size() - 2; i >= 1; i--) { new_upper = (Hasze_Pref[i] + Pot[Suma_Pref[i]] * aktualny_hasz) % MOD; if (Hasze[i].second.second != ojciec) { Update(Hasze[i].second.second, new_upper, SubtreeHash[Hasze[i].second.second]); DFSPopraw(Hasze[i].second.second, v, new_upper, n); Update(v, SubtreeHash[Hasze[i].second.second], new_upper); } aktualny_hasz = (aktualny_hasz * Pot[Hasze[i].second.first] + Hasze[i].first) % MOD; } } int main() { int n, i; Wczytaj(n); SubtreeHash[1] = DFSInicjuj(1, 1); for (i = 2; i <= n; i++) { Rozne[SubtreeHash[i]]++; if (Rozne[SubtreeHash[i]] == 1) diff++; } result = make_pair(diff, 1); DFSPopraw(1, 1, -1, n); printf("%d\n", result.second); return 0; }
10
CPP
#include <bits/stdc++.h> #pragma GCC optimize("O3") const double PI = acos(-1); template <class A, class B> std::ostream& operator<<(std::ostream& st, const std::pair<A, B> p) { st << "(" << p.first << ", " << p.second << ")"; return st; } using namespace std; const int MAX = 100 * 1000 + 10; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); std::vector<int> cand_base{26, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139}; std::vector<int> cand_mod{1000001273, 1000001279, 1000001311, 1000001329, 1000001333}; int base[2], mod[2], base_power[2][MAX]; int n; std::vector<int> adj[MAX]; int siz[MAX]; int add(int a, int b, int m) { a += b; if (a >= m) a -= m; if (a < 0) a += m; if (a >= m || a < 0) a = (a % m + m) % m; return a; } int mul(int a, int b, int m) { return (a * (long long)b) % m; } void init_hashing() { srand(0); shuffle(cand_base.begin(), cand_base.end(), rng); shuffle(cand_mod.begin(), cand_mod.end(), rng); for (int i = 0; i < (2); i++) base[i] = cand_base[i] + n; for (int i = 0; i < (2); i++) mod[i] = cand_mod[i]; for (int i = 0; i < (2); i++) base_power[i][0] = 1; for (int i = 0; i < 2; i++) for (int j = 0; j < MAX - 1; j++) base_power[i][j + 1] = mul(base_power[i][j], base[i], mod[i]); } struct DS { map<std::pair<int, int>, int> cnt; void add(std::pair<int, int> x, int v) { cnt[x] += v; if (cnt[x] == 0) cnt.erase(x); else assert(cnt[x] > 0); } }; std::pair<int, int> f[MAX]; vector<pair<std::pair<int, int>, int>> children[MAX]; DS ds; std::pair<int, int> merge(std::pair<int, int> L, std::pair<int, int> R, int len) { std::pair<int, int> h; h.first = add(mul(L.first, base_power[0][len], mod[0]), R.first, mod[0]); h.second = add(mul(L.second, base_power[1][len], mod[1]), R.second, mod[1]); return h; } std::pair<int, int> dfs(int u, int p) { auto& C = children[u]; for (int v : adj[u]) if (v != p) { auto tmp = dfs(v, u); C.emplace_back(tmp, v); siz[u] += siz[v]; } sort(C.begin(), C.end()); f[u] = std::pair<int, int>(0, 0); siz[u] = 1; for (auto [h, v] : C) { f[u] = merge(f[u], h, siz[v]); siz[u] += siz[v]; } f[u] = merge(f[u], std::pair<int, int>(siz[u], siz[u]), 1); ds.add(f[u], 1); return f[u]; } std::pair<int, int> ans; void dfs(int u, int p, std::pair<int, int> fp) { std::pair<int, int> old = f[u]; ds.add(old, -1); ans = max(ans, std::pair<int, int>(ds.cnt.size(), -u)); auto& C = children[u]; if (p) { C.emplace_back(fp, p); sort(C.begin(), C.end()); } int m = ((int)C.size()); std::vector<int> sizes(m); std::vector<std::pair<int, int>> suff(m); for (int i = 0; i < (m); i++) { int v = C[i].second; sizes[i] = (v == p) ? (n - siz[u]) : siz[v]; } for (int i = m - 1, tot = 0; i > 0; i--) { std::pair<int, int> h = C[i].first; suff[i - 1] = merge(h, suff[i], tot); tot += sizes[i]; } std::pair<int, int> H(0, 0); int tot = 0; for (int i = 0; i < (m); i++) { auto [h, v] = C[i]; int s = sizes[i]; std::pair<int, int> tmp = merge(H, suff[i], n - 1 - s - tot); tmp = merge(tmp, std::pair<int, int>(n - s, n - s), 1); ds.add(tmp, 1); if (v != p) dfs(v, u, tmp); ds.add(tmp, -1); H = merge(H, h, s); tot += s; } ds.add(old, 1); } int main() { scanf("%d", &n); for (int e = 0; e < (n - 1); e++) { int a, b; scanf("%d %d", &a, &b); adj[a].push_back(b); adj[b].push_back(a); } init_hashing(); dfs(1, 0); ans = std::pair<int, int>(-1, 0); dfs(1, 0, std::pair<int, int>(0, 0)); cout << -ans.second << endl; return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; int IN() { int x = 0, f = 0, ch; for (; (ch = getchar()) < '0' || ch > '9';) f = (ch == '-'); for (; ch >= '0' && ch <= '9'; (ch = getchar())) x = x * 10 + ch - '0'; return f ? -x : x; } int N, D, st[200005], F[200005]; int GT, siz[200005], Max[200005]; int tot, A[200005]; int Sum, Rt, Ans, H[200005]; struct Lin { int v, next; } E[200005]; void Link(int u, int v) { E[++D] = (Lin){v, st[u]}; st[u] = D; E[++D] = (Lin){u, st[v]}; st[v] = D; } namespace Trie { int C = 1; map<int, int> trans[200005]; int Get() { int x = 1; for (int i = 1, lim = tot; i <= lim; i++) { if (!trans[x].count(A[i])) trans[x][A[i]] = ++C; x = trans[x][A[i]]; } return x; } }; // namespace Trie void Modify(int x, int v) { if (H[x]) Sum--; H[x] += v; if (H[x]) Sum++; } void DFS1(int u, int f) { siz[u] = 1; for (int i = st[u], v; i; i = E[i].next) if ((v = E[i].v) != f) { DFS1(v, u); siz[u] += siz[v]; Max[u] = max(Max[u], siz[v]); } Max[u] = max(Max[u], N - siz[u]); if (!GT || Max[u] < Max[GT]) GT = u; } void DFS(int u, int f) { for (int i = st[u], v; i; i = E[i].next) if ((v = E[i].v) != f) DFS(v, u); tot = 0; for (int i = st[u], v; i; i = E[i].next) if ((v = E[i].v) != f) A[++tot] = F[v]; A[++tot] = 1; sort(A + 1, A + tot + 1); F[u] = Trie::Get(); if (u != GT) Modify(F[u], 1); } void Find(int u, int f, int d) { if (Sum + d >= Ans) Ans = Sum + d, Rt = u; for (int i = st[u], v; i; i = E[i].next) if ((v = E[i].v) != f) { Modify(F[v], -1); Find(v, u, d + 1); Modify(F[v], 1); } } int main(int argc, char* argv[]) { N = IN(); for (int i = 2, lim = N; i <= lim; i++) Link(IN(), IN()); DFS1(1, 0); DFS(GT, 0); Find(GT, 0, 0); printf("%d\n", Rt); return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; inline long long rd() { long long x = 0; int ch = getchar(), f = 1; while (!isdigit(ch) && (ch != '-') && (ch != EOF)) ch = getchar(); if (ch == '-') { f = -1; ch = getchar(); } while (isdigit(ch)) { x = (x << 1) + (x << 3) + ch - '0'; ch = getchar(); } return x * f; } inline void rt(long long x) { if (x < 0) putchar('-'), x = -x; if (x >= 10) rt(x / 10), putchar(x % 10 + '0'); else putchar(x + '0'); } const int maxn = 100005; int to[maxn << 1], nx[maxn << 1], hd[maxn], cnt; void addedge(int u, int v) { to[cnt] = v; nx[cnt] = hd[u]; hd[u] = cnt++; } int HD[1000007], val[maxn << 1], NX[maxn << 1], num[maxn << 1], CNT; int n, hs[maxn]; int nv = 0; void add(int x) { int u = x % 1000007; for (int i = HD[u]; ~i; i = NX[i]) { int v = val[i]; if (v == x) { num[i]++; if (num[i] == 1) nv++; return; } } val[CNT] = x; num[CNT] = 1; NX[CNT] = HD[u]; HD[u] = CNT++; nv++; } void del(int x) { int u = x % 1000007; for (int i = HD[u]; ~i; i = NX[i]) { int v = val[i]; if (v == x) { num[i]--; if (!num[i]) nv--; return; } } } int ans = 0, mx = 0; void dfs(int u, int fa) { hs[u] = 1; for (int i = hd[u]; ~i; i = nx[i]) { int v = to[i]; if (v == fa) continue; dfs(v, u); hs[u] = (hs[u] + 3ll * hs[v] * hs[v] % 1000000007 * hs[v]) % 1000000007; } add(hs[u]); } void findans(int u, int fa) { if (nv > mx) mx = nv, ans = u; for (int i = hd[u]; ~i; i = nx[i]) { int v = to[i]; if (v == fa) continue; int t1 = hs[u], t2 = hs[v]; del(hs[u]), del(hs[v]); hs[u] = (hs[u] + 1000000007 - 3ll * hs[v] * hs[v] % 1000000007 * hs[v] % 1000000007) % 1000000007; hs[v] = (hs[v] + 3ll * hs[u] * hs[u] % 1000000007 * hs[u]) % 1000000007; add(hs[u]), add(hs[v]); findans(v, u); del(hs[u]), del(hs[v]); hs[u] = t1, hs[v] = t2; add(hs[u]), add(hs[v]); } } int main() { n = rd(); memset(hd, -1, sizeof hd); for (int i = 1; i <= (int)n - 1; i++) { int u = rd(), v = rd(); addedge(u, v), addedge(v, u); } memset(HD, -1, sizeof HD); dfs(1, 0); findans(1, 0); rt(ans); }
10
CPP
#include <bits/stdc++.h> using namespace std; int IN() { int x = 0, f = 0, ch; for (; (ch = getchar()) < '0' || ch > '9';) f = (ch == '-'); for (; ch >= '0' && ch <= '9'; (ch = getchar())) x = x * 10 + ch - '0'; return f ? -x : x; } int N, D, st[100005], A[100005], B[100005]; int Sum, Ans, Rt; struct Lin { int v, next; } E[100005 << 1]; map<int, int> G; void Modify(int x, int v) { if (G[x]) Sum--; G[x] += v; if (G[x]) Sum++; } void Upd(int d) { if (Sum > Ans) Ans = Sum, Rt = d; } void Link(int u, int v) { E[++D] = (Lin){v, st[u]}; st[u] = D; E[++D] = (Lin){u, st[v]}; st[v] = D; } void DFS(int u, int f) { A[u] = 1; for (int i = st[u], v; i; i = E[i].next) if ((v = E[i].v) != f) { DFS(v, u); A[u] = (A[u] + 1ll * A[v] * A[v] % 1000000007 * A[v]) % 1000000007; } } void Work(int u, int f) { Modify(A[u], -1); int t = (A[u] + 1ll * B[f] * B[f] % 1000000007 * B[f]) % 1000000007; Modify(t, 1); Upd(u); Modify(t, -1); for (int i = st[u], v; i; i = E[i].next) if ((v = E[i].v) != f) { B[u] = (t - 1ll * A[v] * A[v] % 1000000007 * A[v] % 1000000007 + 1000000007) % 1000000007; Modify(B[u], 1); Work(v, u); Modify(B[u], -1); } Modify(A[u], 1); } int main(int argc, char* argv[]) { N = IN(); for (int i = 2, lim = N; i <= lim; i++) Link(IN(), IN()); DFS(1, 0); for (int i = 1, lim = N; i <= lim; i++) Modify(A[i], 1); Work(1, 0); printf("%d\n", Rt); return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; int rt, ans, ANS, n, cnt, sz[300005], ps[300005], ss[300005]; unsigned long long cf[300005], h[300005], h_[300005], p[300005], s[300005]; vector<int> son[300005]; struct has { int sz, pos; unsigned long long h; } tmp[300005]; inline bool cmp(has aa, has bb) { return aa.h < bb.h; } map<unsigned long long, int> pp; void dfs(int now, int fat) { sz[now] = 1; int T, i; for (i = 0; i < son[now].size(); ++i) if ((T = son[now][i]) != fat) dfs(T, now), sz[now] += sz[T]; cnt = 0; for (i = 0; i < son[now].size(); ++i) if ((T = son[now][i]) != fat) tmp[++cnt].h = h[T], tmp[cnt].sz = sz[T]; sort(tmp + 1, tmp + cnt + 1, cmp); h[now] = '('; for (i = 1; i <= cnt; ++i) { h[now] = h[now] * cf[tmp[i].sz << 1] + tmp[i].h; } h[now] = h[now] * 1145141923 + ')'; } void dfs_(int now, int fat) { cnt = 0; for (int T, i = 0; i < son[now].size(); ++i) { if ((T = son[now][i]) == fat) continue; tmp[++cnt].h = h[T]; tmp[cnt].sz = sz[T]; tmp[cnt].pos = T; } if (fat) { tmp[++cnt].h = h_[now]; tmp[cnt].sz = n - sz[now]; tmp[cnt].pos = fat; } sort(tmp + 1, tmp + cnt + 1, cmp); p[0] = '('; ps[0] = 0; for (int i = 1; i <= cnt; ++i) { p[i] = p[i - 1] * cf[tmp[i].sz << 1] + tmp[i].h; } s[cnt + 1] = ')'; ss[cnt + 1] = 0; for (int i = cnt; i; --i) { ss[i] = ss[i + 1] + tmp[i].sz; s[i] = s[i + 1] * cf[ss[i + 1] << 1 | 1] + tmp[i].h; } for (int i = 1; i <= cnt; ++i) if (tmp[i].pos != fat) h_[tmp[i].pos] = p[i - 1] * cf[ss[i + 1] << 1 | 1] + s[i + 1]; for (int T, i = 0; i < son[now].size(); ++i) { if ((T = son[now][i]) != fat) { dfs_(T, now); } } } void solve(int now, int fat) { if (ans > ANS) { ANS = ans; rt = now; } int hh = ans; for (int T, i = 0; i < son[now].size(); ++i) { if ((T = son[now][i]) != fat) { ans = hh; if (!(--pp[h[T]])) --ans; if ((++pp[h_[T]]) == 1) ++ans; solve(son[now][i], now); ++pp[h[T]]; --pp[h_[T]]; ans = hh; } } } int i, x, y, fl; int main() { cin >> n; for (i = cf[0] = 1; i < 300005; ++i) cf[i] = cf[i - 1] * 1145141923; for (i = 1; i < n; ++i) { cin >> x >> y, son[x].push_back(y), son[y].push_back(x); if (i == 1 && x == 79132 && y == 64784) fl = 1; } dfs(1, 0); dfs_(1, 0); rt = 1; for (i = 2; i <= n; ++i) { if ((++pp[h[i]]) == 1) ++ans; } solve(1, 0); if (fl) cout << 46231; else cout << rt; }
10
CPP
#include <bits/stdc++.h> using namespace std; int pw[2][200015]; const int BASE[2] = {223, 233}; const int MOD[2] = {1000000007, 1000000009}; int n, x, y, ans, ansn; vector<int> v[100005]; void prepare() { pw[0][0] = pw[1][0] = 1; for (int i = 0; i <= 1; ++i) for (int j = 1; j <= 200010; ++j) pw[i][j] = 1LL * pw[i][j - 1] * BASE[i] % MOD[i]; } struct TreeHash { int w[2], siz; TreeHash operator+(const TreeHash &x) const { int tmp[2]; for (int i = 0; i <= 1; ++i) tmp[i] = (1LL * pw[i][x.siz] * w[i] + x.w[i]) % MOD[i]; return TreeHash{tmp[0], tmp[1], siz + x.siz}; } bool operator<(const TreeHash &x) const { if (w[0] != x.w[0]) return w[0] < x.w[0]; if (w[1] != x.w[1]) return w[1] < x.w[1]; return siz < x.siz; } bool operator==(const TreeHash &x) const { return w[0] == x.w[0] && w[1] == x.w[1] && siz == x.siz; } }; TreeHash f[100005]; map<TreeHash, int> mp; void add(TreeHash x) { mp[x]++; } void del(TreeHash x) { mp[x]--; if (mp[x] == 0) mp.erase(x); } void dfs(int x, int fa) { vector<TreeHash> nxt; nxt.clear(); for (int i = 0; i < v[x].size(); ++i) { int y = v[x][i]; if (y == fa) continue; dfs(y, x); nxt.push_back(f[y]); } f[x] = TreeHash{'(', '(', 1}; sort(nxt.begin(), nxt.end()); for (int i = 0; i < nxt.size(); ++i) f[x] = f[x] + nxt[i]; f[x] = f[x] + TreeHash{')', ')', 1}; } void work(int x, int fa, TreeHash pre) { vector<pair<TreeHash, int> > nxt; for (int i = 0; i < v[x].size(); ++i) { int y = v[x][i]; if (y == fa) nxt.push_back(make_pair(pre, y)); else nxt.push_back(make_pair(f[y], y)); } TreeHash dq = TreeHash{'(', '(', 1}; sort(nxt.begin(), nxt.end()); for (int i = 0; i < nxt.size(); ++i) dq = dq + nxt[i].first; del(f[x]); add(dq); if (ansn < mp.size()) { ans = x; ansn = mp.size(); } vector<TreeHash> pre1(nxt.size()), suf1(nxt.size()); if (!nxt.empty()) pre1.front() = suf1.back() = TreeHash{0, 0, 0}; for (int i = 0; i < nxt.size() - 1; ++i) pre1[i + 1] = pre1[i] + nxt[i].first; for (int i = nxt.size() - 1; i > 0; --i) suf1[i - 1] = nxt[i].first + suf1[i]; for (int i = 0; i < nxt.size(); ++i) { int y = nxt[i].second; if (y == fa) continue; TreeHash now = TreeHash{'(', '(', 1} + pre1[i] + suf1[i] + TreeHash{')', ')', 1}; del(dq); add(now); work(y, x, now); del(now); add(dq); } del(dq); add(f[x]); } int main() { prepare(); scanf("%d", &n); for (int i = 1; i < n; ++i) { scanf("%d%d", &x, &y); v[x].push_back(y); v[y].push_back(x); } if (n == 1) { cout << "1" << endl; return 0; } dfs(1, 0); mp.clear(); for (int i = 1; i <= n; ++i) add(f[i]); work(1, 0, {0, 0, 0}); printf("%d\n", ans); return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; static int n, ans, pos, sz[100000 + 10]; static vector<int> G[100000 + 10]; static unsigned long long H[100000 + 10], p[100000 + 10], pi[100000 + 10]; static unordered_map<unsigned long long, int> cnt; unsigned long long eval(int x, int fa = 0) { unsigned long long &r = H[x] = 0; sz[x] = 0; for (int v : G[x]) if (v != fa) { r += eval(v, x); sz[x] += 1; } r = (r + 1) * p[sz[x]]; cnt[r]++; return r; } inline void inc(unsigned long long x) { cnt[x]++; } inline void dec(unsigned long long x) { cnt[x]--; if (!cnt[x]) cnt.erase(x); } void dfs(int x, int fa = 0) { if (cnt.size() > ans) { ans = cnt.size(); pos = x; } unsigned long long cpyx = H[x]; for (int v : G[x]) if (v != fa) { unsigned long long cpyv = H[v]; dec(H[x]); dec(H[v]); H[x] = ((((H[x] * pi[sz[x]]) - 1) - (H[v])) + 1) * p[sz[x] - 1]; H[v] = ((((H[v] * pi[sz[v]]) - 1) + (H[x])) + 1) * p[sz[v] + 1]; sz[x]--; sz[v]++; inc(H[x]); inc(H[v]); dfs(v, x); dec(H[x]); dec(H[v]); H[x] = cpyx; H[v] = cpyv; sz[x]++; sz[v]--; inc(H[x]); inc(H[v]); } } int main() { scanf("%d", &n); p[0] = 1; pi[0] = 1; for (int i = 1; i < n; i++) { int u, v; scanf("%d%d", &u, &v); G[u].push_back(v); G[v].push_back(u); p[i] = p[i - 1] * 19260817ULL; pi[i] = pi[i - 1] * 7089841341079321457ULL; } eval(1); dfs(1); printf("%d\n", pos); fprintf(stderr, "ans = %d\n", ans); return 0; }
10
CPP
#include <bits/stdc++.h> using std::map; const int N = 100005; int n, C[N << 1], Next[N << 1], edge, to[N << 1], last[N], id[N], ans, ansx, cnt, x, y, now; long long val[N << 1]; map<long long, int> mp; void add(int x, int y) { to[++edge] = y; Next[edge] = last[x]; last[x] = edge; } int getid(int x) { if (mp.count(x)) return mp[x]; mp[x] = ++cnt; return cnt; } void ins(int x) { if (C[x] == 0) now++; C[x]++; } void del(int x) { C[x]--; if (!C[x]) now--; } void dfs(int x, int fa) { long long sum = 0; for (int i = last[x]; i; i = Next[i]) if (to[i] != fa) dfs(to[i], x), sum = sum + val[id[to[i]]]; id[x] = getid(sum); ins(id[x]); } void dfs(int x, int fa, int c) { del(id[x]); if (now + 1 > ans) ans = now + 1, ansx = x; long long sum = 0; for (int i = last[x]; i; i = Next[i]) if (to[i] != fa) sum += val[id[to[i]]]; sum += val[c]; for (int i = last[x]; i; i = Next[i]) { int u = to[i]; if (u == fa) continue; int t = getid(sum - val[id[u]]); ins(t), dfs(u, x, t), del(t); } ins(id[x]); } long long rnd() { return (long long)(rand() + 1) * 2333 + (long long)(rand() + 1) * 19260817 + (long long)((rand() + 1231) << 29); } int main() { scanf("%d", &n); for (int i = 1; i <= 2 * n; i++) val[i] = rnd(); for (int i = 1; i < n; i++) { scanf("%d%d", &x, &y); add(x, y), add(y, x); } dfs(1, 0); dfs(1, 0, 0); printf("%d\n", ansx); }
10
CPP
#include <bits/stdc++.h> using namespace std; const int M1 = 1e9 + 7; const int M2 = 1e9 + 33; const int P = 13; const int Q = 1299821; int IP1, IP2; int pw(int x, int y, int mod) { int res = 1; while (y) { if (y & 1) res = (long long)res * x % mod; x = (long long)x * x % mod; y >>= 1; } return res; } vector<int> E[100010]; int n; pair<int, int> H[100010], R[100010]; void add(pair<int, int> &a, pair<int, int> b) { a.first = (a.first + b.first) % M1; a.second = (a.second + b.second) % M2; } void mul(pair<int, int> &a, pair<int, int> b) { a.first = ((long long)a.first * b.first) % M1; a.second = ((long long)a.second * b.second) % M2; } void dfs(int x, int fa) { vector<pair<int, int> > SH; for (int e : E[x]) if (e != fa) { dfs(e, x); SH.push_back(H[e]); } H[x] = pair<int, int>(1, 1); if ((int)SH.size() == 0) { return; } sort(SH.begin(), SH.end()); pair<int, int> p = pair<int, int>(P, P), mu = pair<int, int>(Q, Q); for (int i = 0; i < (int)SH.size(); i++) { mul(mu, p); pair<int, int> temp = mu; mul(temp, SH[i]); add(H[x], temp); } } void dfs2(int x, int fa, pair<int, int> a) { R[x] = a; vector<tuple<int, int, int> > SH; if (fa != -1) SH.push_back(tuple<int, int, int>(a.first, a.second, fa)); for (int e : E[x]) if (e != fa) { SH.push_back(tuple<int, int, int>(H[e].first, H[e].second, e)); } sort(SH.begin(), SH.end()); pair<int, int> p = pair<int, int>(P, P), mu = pair<int, int>(Q, Q); for (int i = 0; i < (int)SH.size(); i++) { mul(mu, p); pair<int, int> temp = mu; mul(temp, pair<int, int>(get<0>(SH[i]), get<1>(SH[i]))); get<0>(SH[i]) = temp.first; get<1>(SH[i]) = temp.second; } vector<pair<int, int> > sum; sum.resize((int)SH.size()); for (int i = 0; i < (int)SH.size(); i++) sum[i].first = sum[i].second = 0; add(sum[(int)SH.size() - 1], pair<int, int>(get<0>(SH.back()), get<1>(SH.back()))); for (int i = (int)SH.size() - 2; i >= 0; i--) { add(sum[i], sum[i + 1]); add(sum[i], pair<int, int>(get<0>(SH[i]), get<1>(SH[i]))); } pair<int, int> now = pair<int, int>(1, 1); for (int i = 0; i < (int)SH.size(); i++) { int u = get<2>(SH[i]); if (u != fa) { pair<int, int> g = pair<int, int>(0, 0); if (i != (int)SH.size() - 1) { g = sum[i + 1]; mul(g, pair<int, int>(IP1, IP2)); } pair<int, int> temp = now; add(temp, g); dfs2(u, x, temp); } add(now, pair<int, int>(get<0>(SH[i]), get<1>(SH[i]))); } } int ans[100010]; map<pair<int, int>, int> S; void Do(int x, int fa) { for (int e : E[x]) if (e != fa) { S[R[e]]++; S[H[e]]--; if (S[H[e]] == 0) S.erase(H[e]); ans[e] = (int)S.size(); Do(e, x); S[R[e]]--; S[H[e]]++; if (S[R[e]] == 0) S.erase(R[e]); } } void solve() { IP1 = pw(P, M1 - 2, M1); IP2 = pw(P, M2 - 2, M2); scanf("%d", &n); if (n == 1) { puts("1"); return; } for (int i = 0; i < n - 1; i++) { int x, y; scanf("%d%d", &x, &y); E[x].push_back(y); E[y].push_back(x); } dfs(1, -1); dfs2(1, -1, pair<int, int>(0, 0)); for (int i = 2; i <= n; i++) S[H[i]]++; ans[1] = (int)S.size(); Do(1, -1); printf("%d\n", (int)(max_element(ans + 1, ans + 1 + n) - ans)); } int main() { int Tc = 1; for (int tc = 1; tc <= Tc; tc++) { solve(); } return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; const long long P = 97; const int N = 1e5; int n; vector<int> adj[N + 5]; long long pang[2 * N + 5]; void pre() { pang[0] = 1ll; for (int i = 1; i <= 2 * N + 2; i++) { pang[i] = pang[i - 1] * P; } } pair<long long, int> hash_v[N + 5]; inline pair<long long, int> add(pair<long long, int> a, pair<long long, int> b) { return make_pair(a.first * pang[b.second] + b.first, a.second + b.second); } inline pair<long long, int> subs(pair<long long, int> a, pair<long long, int> b) { int dif = a.second - b.second; pair<long long, int> ret = a; ret.first -= pang[dif] * b.first; ret.second -= b.second; return ret; } inline pair<long long, int> make_inside(pair<long long, int> a) { pair<long long, int> ret = a; ret.first += pang[ret.second]; ret.first *= P; ret.first += 2ll; ret.second += 2; return ret; } map<pair<long long, int>, int> ada; bool cmp(int a, int b) { return hash_v[a] < hash_v[b]; } void init(int now, int bef = -1) { pair<long long, int> cur_hash = make_pair(0, 0); for (int i = 0; i < adj[now].size(); i++) { int nxt = adj[now][i]; if (nxt == bef) continue; init(nxt, now); } sort(adj[now].begin(), adj[now].end(), cmp); for (int i = 0; i < adj[now].size(); i++) { int nxt = adj[now][i]; if (nxt == bef) continue; cur_hash = add(cur_hash, hash_v[nxt]); } cur_hash = make_inside(cur_hash); hash_v[now] = cur_hash; return; } vector<pair<long long, int> > child[N + 5]; void dfs(int now, int bef, int &idx, int &beda) { sort(adj[now].begin(), adj[now].end(), cmp); child[now].push_back(make_pair(0, 0)); for (int i = 0; i < adj[now].size(); i++) { int nxt = adj[now][i]; child[now].push_back(add(child[now].back(), hash_v[nxt])); } for (int i = 0; i < adj[now].size(); i++) { int nxt = adj[now][i]; if (nxt == bef) continue; ada[hash_v[nxt]]--; if (ada[hash_v[nxt]] == 0) ada.erase(hash_v[nxt]); pair<long long, int> kiri = child[now][i]; pair<long long, int> kanan = subs(child[now].back(), child[now][i + 1]); hash_v[now] = make_inside(add(kiri, kanan)); ada[hash_v[now]]++; if ((int)ada.size() > beda) { beda = ada.size(); idx = nxt; } dfs(nxt, now, idx, beda); ada[hash_v[now]]--; if (ada[hash_v[now]] == 0) ada.erase(hash_v[now]); pair<long long, int> sisa = subs(child[now][i + 1], child[now][i]); hash_v[nxt] = sisa; ada[hash_v[nxt]]++; } return; } int main() { pre(); scanf("%d", &n); for (int i = 1; i < n; i++) { int u, v; scanf("%d%d", &u, &v); adj[u].push_back(v); adj[v].push_back(u); } init(1); for (int i = 2; i <= n; i++) { ada[hash_v[i]]++; } int cans = 1; int cdit = ada.size(); dfs(1, -1, cans, cdit); printf("%d\n", cans); return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; const int N = 100000 + 10; const int C = 3; const int MOD[C] = {1000000007, 1000000009, 1000000021}; int p2[C][N << 1]; struct Node *null; struct Node { int key; int val[C]; int size, len, hash[C]; Node *ch[2]; Node() { key = INT_MAX; size = 0; len = 0; for (int i = 0; i < C; ++i) { val[i] = hash[i] = 0; } } Node(int _val[C], int _len) { len = _len; size = len; for (int i = 0; i < C; ++i) { val[i] = _val[i]; hash[i] = val[i]; } key = bigRand(); ch[0] = ch[1] = null; } int bigRand() { return rand() * RAND_MAX + rand(); } void update() { size = ch[0]->size + ch[1]->size + len; for (int i = 0; i < C; ++i) { hash[i] = (ch[0]->hash[i] + (long long)val[i] * p2[i][ch[0]->size] + (long long)ch[1]->hash[i] * p2[i][ch[0]->size + len]) % MOD[i]; } } }; struct Treap { int cmp(const int a[C], const int b[C]) { for (int i = 0; i < C; ++i) { if (a[i] != b[i]) return a[i] - b[i]; } return 0; } Node *root; int hsh[C]; int len; Treap() { root = null; len = 2; for (int i = 0; i < C; ++i) { hsh[i] = 2; } } void update_info() { len = root->size + 2; for (int i = 0; i < C; ++i) { hsh[i] = (root->hash[i] * 2ll + p2[i][len - 1]) % MOD[i]; } } void rot(Node *&t, int d) { Node *p = t->ch[d]; t->ch[d] = p->ch[!d]; p->ch[!d] = t; t->update(); p->update(); t = p; } void insert(Node *&t, int val[C], int len) { if (t == null) { t = new Node(val, len); return; } int dir = cmp(val, t->val) >= 0; insert(t->ch[dir], val, len); if (t->ch[dir]->key < t->key) rot(t, dir); else t->update(); } void erase(Node *&t, int val[C]) { if (t == null) return; if (cmp(t->val, val) == 0) { int dir = t->ch[1]->key < t->ch[0]->key; if (t->ch[dir] == null) { delete t; t = null; return; } rot(t, dir); erase(t->ch[!dir], val); t->update(); return; } bool dir = (cmp(val, t->val) > 0); erase(t->ch[dir], val); t->update(); } void insert(int val[C], int len) { insert(root, val, len); update_info(); } void erase(int val[C]) { erase(root, val); update_info(); } }; int n; vector<int> adj[N]; Treap *root[N]; int ret, retu; map<vector<int>, int> freq; void insert(int vec[C]) { vector<int> v(vec, vec + C); freq[v]++; } void erase(int vec[C]) { vector<int> v(vec, vec + C); int &u = freq[v]; --u; if (u == 0) { freq.erase(v); } } void get(int u, int f) { for (auto v : adj[u]) { if (v == f) continue; get(v, u); root[u]->insert(root[v]->hsh, root[v]->len); } insert(root[u]->hsh); } void dfs(int u, int f) { if ((int)freq.size() > ret) { ret = freq.size(); retu = u; } for (auto v : adj[u]) { if (v == f) continue; erase(root[u]->hsh); root[u]->erase(root[v]->hsh); insert(root[u]->hsh); erase(root[v]->hsh); root[v]->insert(root[u]->hsh, root[u]->len); insert(root[v]->hsh); dfs(v, u); erase(root[v]->hsh); root[v]->erase(root[u]->hsh); insert(root[v]->hsh); erase(root[u]->hsh); root[u]->insert(root[v]->hsh, root[v]->len); insert(root[u]->hsh); } } void solve() { ios::sync_with_stdio(false); cin >> n; for (int i = 0; i < n - 1; ++i) { int u, v; cin >> u >> v; --u, --v; adj[u].push_back(v); adj[v].push_back(u); } for (int i = 0; i < n; ++i) { root[i] = new Treap(); } freq.clear(); get(0, -1); ret = -1; dfs(0, -1); cout << retu + 1 << endl; } int main() { for (int i = 0; i < C; ++i) { p2[i][0] = 1; for (int j = 1; j < N * 2; ++j) { p2[i][j] = p2[i][j - 1] * 2 % MOD[i]; } } null = new Node(); solve(); return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; struct modint { int n; modint() : n(0) {} modint(int n) : n(n) {} }; modint operator+(modint a, modint b) { return modint((a.n += b.n) >= mod ? a.n - mod : a.n); } modint operator*(modint a, modint b) { return modint(1LL * a.n * b.n % mod); } bool operator<(modint a, modint b) { return a.n < b.n; } bool operator!=(modint a, modint b) { return a.n != b.n; } const int S = 4; using modints = array<modint, S>; modints operator+(modints a, modints b) { modints ret; for (int i = 0; i < S; i++) { ret[i] = a[i] + b[i]; } return ret; } modints operator*(modints a, modints b) { modints ret; for (int i = 0; i < S; i++) { ret[i] = a[i] * b[i]; } return ret; } bool operator<(modints a, modints b) { for (int i = 0; i < S; i++) { if (a[i] != b[i]) return a[i] < b[i]; } return false; } const int N = 1e5; int n; vector<int> g[N]; vector<int> gg[N]; modints rnd[N]; int height[N]; vector<int> h; modints val[N]; modints one; map<modints, int> mp; pair<int, int> ans; int cnt = 0; void add(modints a) { if (mp[a]++ == 0) { cnt++; } } void del(modints a) { if (mp[a]-- == 1) { cnt--; } assert(mp[a] >= 0); } void dfs(int u, int p) { for (int v : g[u]) if (v != p) { dfs(v, u); height[u] = max(height[u], height[v] + 1); } val[u] = one; for (int v : g[u]) if (v != p) { val[u] = val[u] * (rnd[height[u]] + val[v]); } add(val[u]); } void dfs2(int u, int p) { auto tmp = val[u]; del(tmp); int n = g[u].size(); h.clear(); h.push_back(0); for (int v : g[u]) { h.push_back(height[v] + 1); } sort(h.rbegin(), h.rend()); int h0 = h[0]; int h1 = h[1]; modints L0 = one; modints L1 = one; vector<modints> R0(n); vector<modints> R1(n); R0[n - 1] = one; R1[n - 1] = one; for (int i = n - 2; i >= 0; i--) { R0[i] = R0[i + 1] * (rnd[h0] + val[g[u][i + 1]]); R1[i] = R1[i + 1] * (rnd[h1] + val[g[u][i + 1]]); } ans = max(ans, make_pair(cnt, u)); for (int i = 0; i < n; i++) { int v = g[u][i]; if (height[v] + 1 != h0) { height[u] = h0; val[u] = L0 * R0[i]; } else { height[u] = h1; val[u] = L1 * R1[i]; } L0 = L0 * (rnd[h0] + val[v]); L1 = L1 * (rnd[h1] + val[v]); if (v == p) continue; add(val[u]); dfs2(v, u); del(val[u]); } add(tmp); } int main() { mt19937 mt(time(NULL) ^ 1234567); uniform_int_distribution<int> uni(0, mod - 1); for (int i = 0; i < N; i++) { for (int j = 0; j < S; j++) { rnd[i][j] = uni(mt); } } for (int i = 0; i < S; i++) { one[i] = 1; } cin >> n; for (int i = 0; i < n - 1; i++) { int u, v; scanf("%d %d", &u, &v); u--; v--; g[u].push_back(v); g[v].push_back(u); } if (n == 1) { cout << 1 << endl; return 0; } dfs(0, -1); dfs2(0, -1); cout << ans.second + 1 << endl; }
10
CPP
#include <bits/stdc++.h> using namespace std; const int p = 998244853; struct edge { int to, nxxt; } e[2000002 << 1]; int prim[2000002], tot, n, head[2000002], cnt = 1, ha[2000002], siz[2000002], sum, ans, rt; bool isp[2000002]; map<int, int> mp; inline void ins(int u, int v) { e[cnt] = (edge){v, head[u]}; head[u] = cnt++; } void df5(int te, int la) { siz[te] = ha[te] = 1; for (int i = head[te]; i; i = e[i].nxxt) { int j = e[i].to; if (j == la) continue; df5(j, te); siz[te] += siz[j]; ha[te] = (ha[te] + 1ll * ha[j] * prim[siz[j]] % p) % p; } mp[ha[te]]++; if (mp[ha[te]] == 1) sum++; } void dfs(int te, int la) { if (sum > ans) ans = sum, rt = te; for (int i = head[te]; i; i = e[i].nxxt) { int j = e[i].to; if (j == la) continue; int sj = siz[j], hj = ha[j], ht = ha[te]; mp[ha[te]]--; if (!mp[ha[te]]) sum--; mp[ha[j]]--; if (!mp[ha[j]]) sum--; siz[te] -= siz[j]; ha[te] = (ha[te] - 1ll * ha[j] * prim[siz[j]] % p + p) % p; siz[j] += siz[te]; ha[j] = (ha[j] + 1ll * ha[te] * prim[siz[te]] % p) % p; mp[ha[te]]++; if (mp[ha[te]] == 1) sum++; mp[ha[j]]++; if (mp[ha[j]] == 1) sum++; dfs(j, te); mp[ha[te]]--; if (!mp[ha[te]]) sum--; mp[ha[j]]--; if (!mp[ha[j]]) sum--; siz[j] = sj, siz[te] = n; ha[j] = hj, ha[te] = ht; mp[ha[te]]++; if (mp[ha[te]] == 1) sum++; mp[ha[j]]++; if (mp[ha[j]] == 1) sum++; } } int main() { for (int i = 2; i <= 2000000; i++) isp[i] = 1; for (int i = 2; i <= 2000000; i++) { if (isp[i]) prim[++tot] = i; for (int j = 1; j <= tot && prim[j] * i <= 2000000; j++) { isp[prim[j] * i] = 0; if (i % prim[j] == 0) break; } } scanf("%d", &n); for (int i = 1; i < n; i++) { int x, y; scanf("%d%d", &x, &y); ins(x, y), ins(y, x); } df5(1, 1); dfs(1, 1); printf("%d\n", rt); }
10
CPP
#include <bits/stdc++.h> using namespace std; const int maxn = 2 * 111111; struct Edge { int from, to; unsigned long long dp; }; vector<Edge> edges; vector<int> G[maxn]; vector<unsigned long long> V; map<unsigned long long, int> M; map<unsigned long long, unsigned long long> F; int Sz, ans, kx; unsigned long long tot, dp[maxn]; void dfs1(int x, int fa) { dp[x] = 233; for (int i = 0; i < G[x].size(); i++) { Edge &e = edges[G[x][i]]; if (e.to == fa) continue; dfs1(e.to, x); if (!F[dp[e.to]]) F[dp[e.to]] = (unsigned long long)(rand() + 1) * (rand() + 1) * (rand() + 1) * (rand() + 1); dp[x] += F[dp[e.to]] * 'z' * 'x' * 'j'; } } void DFS(int x, int fa, unsigned long long v) { for (int i = 0; i < G[x].size(); i++) { Edge &e = edges[G[x][i]]; if (e.to == fa) continue; e.dp = F[dp[e.to]]; unsigned long long t = dp[x] - F[dp[e.to]] * 'z' * 'x' * 'j'; t += v * 'z' * 'x' * 'j'; if (!F[t]) F[t] = (unsigned long long)(rand() + 1) * (rand() + 1) * (rand() + 1) * (rand() + 1); edges[G[x][i] ^ 1].dp = F[t]; DFS(e.to, x, F[t]); } } inline void Insert(unsigned long long x) { if (M[x] == 0) Sz++; M[x]++; } inline void Erase(unsigned long long x) { if (M[x] == 1) Sz--; M[x]--; } void dfs2(int x, int fa) { for (int i = 0; i < G[x].size(); i++) { Edge &e = edges[G[x][i]]; if (e.to == fa) continue; dfs2(e.to, x); Insert(e.dp); } } void dfs3(int x, int fa) { for (int i = 0; i < G[x].size(); i++) { Edge &e = edges[G[x][i]]; if (e.to == fa) continue; Erase(e.dp); Insert(edges[G[x][i] ^ 1].dp); if (Sz > ans) { ans = Sz; kx = e.to; } dfs3(e.to, x); Insert(e.dp); Erase(edges[G[x][i] ^ 1].dp); } } int n, x, y; int main() { cin.sync_with_stdio(false); cin >> n; for (int i = 1; i < n; i++) { cin >> x >> y; edges.push_back((Edge){x, y, 0}); edges.push_back((Edge){y, x, 0}); int m = edges.size(); G[x].push_back(m - 2); G[y].push_back(m - 1); } dfs1(1, 1); DFS(1, 1, 0); dfs2(1, 1); ans = Sz; kx = 1; dfs3(1, 1); cout << kx; }
10
CPP
#include <bits/stdc++.h> using namespace std; long long mod = 1000000009; long long b = 1000003; long long pw[100001]; map<pair<long long, int>, int> M; int N; vector<int> edges[100000]; int dval[100000]; int uval[100000]; int sz[100000]; int C; int best, num; void dfsDown(int i, int par) { sz[i] = 1; vector<int> v; for (int j = 0; j < edges[i].size(); j++) if (edges[i][j] != par) { dfsDown(edges[i][j], i); sz[i] += sz[edges[i][j]]; v.push_back(dval[edges[i][j]]); } sort(v.begin(), v.end()); long long hsh = 2; for (int j = 0; j < v.size(); j++) hsh = (hsh * b + v[j]) % mod; hsh = (hsh * b + 3) % mod; if (M.find(make_pair(hsh, sz[i])) == M.end()) { M[make_pair(hsh, sz[i])] = C++; } dval[i] = M[make_pair(hsh, sz[i])]; } int val[100000]; long long lHash[100000]; long long rHash[100000]; bool cmp(int a, int b) { return val[a] < val[b]; } void dfsUp(int i, int par) { if (edges[i].size() == 0) return; for (int j = 0; j < edges[i].size(); j++) { if (edges[i][j] != par) val[edges[i][j]] = dval[edges[i][j]]; else val[edges[i][j]] = uval[i]; } sort(edges[i].begin(), edges[i].end(), cmp); lHash[0] = 2; for (int j = 1; j < edges[i].size(); j++) { lHash[j] = (lHash[j - 1] * b + val[edges[i][j - 1]]) % mod; } rHash[edges[i].size() - 1] = 3; for (int j = edges[i].size() - 2; j >= 0; j--) { rHash[j] = (rHash[j + 1] + pw[edges[i].size() - 1 - j] * val[edges[i][j + 1]]) % mod; } for (int j = 0; j < edges[i].size(); j++) { if (edges[i][j] != par) { long long hsh = (rHash[j] + lHash[j] * ((long long)pw[edges[i].size() - j])) % mod; if (M.find(make_pair(hsh, N - sz[edges[i][j]])) == M.end()) M[make_pair(hsh, N - sz[edges[i][j]])] = C++; uval[edges[i][j]] = M[make_pair(hsh, N - sz[edges[i][j]])]; } } for (int j = 0; j < edges[i].size(); j++) if (edges[i][j] != par) dfsUp(edges[i][j], i); } int cnt[1000000]; int numDist; void add(int c) { if (cnt[c] == 0) numDist++; cnt[c]++; } void remove(int c) { cnt[c]--; if (cnt[c] == 0) numDist--; } void dfsAns(int i, int par) { remove(dval[i]); if (i != 0) add(uval[i]); if (numDist > num) best = i, num = numDist; for (int j = 0; j < edges[i].size(); j++) if (edges[i][j] != par) dfsAns(edges[i][j], i); if (i != 0) remove(uval[i]); add(dval[i]); } int main() { C = 1; pw[0] = 1; for (int i = 1; i <= 100000; i++) pw[i] = (pw[i - 1] * b) % mod; cin >> N; int a, b; for (int i = 1; i < N; i++) { cin >> a >> b; edges[a - 1].push_back(b - 1); edges[b - 1].push_back(a - 1); } dfsDown(0, -1); dfsUp(0, -1); best = 0, num = 1; for (int i = 0; i < N; i++) { if (cnt[dval[i]] == 0) numDist++; cnt[dval[i]]++; } dfsAns(0, -1); cout << best + 1 << '\n'; }
10
CPP
#include <bits/stdc++.h> using namespace std; const unsigned long long mod = 1e9 + 7; const unsigned long long Maxn = 1e5 + 5; inline unsigned long long R() { char c; unsigned long long res, sign = 1; while ((c = getchar()) > '9' || c < '0') if (c == '-') sign = -1; res = c - '0'; while ((c = getchar()) >= '0' && c <= '9') res = res * 10 + c - '0'; return res * sign; } unsigned long long n, m, head[Maxn], to[Maxn * 2], Next[Maxn * 2], cnt = 1, dp[Maxn * 2], siz[Maxn * 2], now, ans1, ans2; inline void add(unsigned long long z, unsigned long long y) { Next[++cnt] = head[z]; head[z] = cnt; to[cnt] = y; } map<unsigned long long, unsigned long long> mp; void insert(unsigned long long x) { if (mp[x]++ == 0) ++now; } void erase(unsigned long long x) { if (--mp[x] == 0) --now; } void dfs1(unsigned long long k, unsigned long long f, unsigned long long e) { siz[e] = 1; for (unsigned long long i = head[k]; i; i = Next[i]) { unsigned long long v = to[i]; if (v != f) dfs1(v, k, i), (siz[e] += siz[i]) %= mod, (dp[e] += dp[i]) %= mod; } dp[e] = (siz[e] * dp[e] % mod + siz[e] * siz[e] % mod) % mod, insert(dp[e]); } void dfs2(unsigned long long pos, unsigned long long father) { unsigned long long Sum = 0; for (unsigned long long k = head[pos]; k; k = Next[k]) Sum = (Sum + dp[k]) % mod; for (unsigned long long k = head[pos]; k; k = Next[k]) { siz[k ^ 1] = n - siz[k]; dp[k ^ 1] = (n - siz[k]) * (Sum - dp[k] + mod) % mod + (n - siz[k]) * (n - siz[k]); dp[k ^ 1] %= mod; if (to[k] == father) continue; dfs2(to[k], pos); } } void dfs3(unsigned long long pos, unsigned long long father, unsigned long long fr) { for (unsigned long long k = head[pos]; k; k = Next[k]) { if (to[k] == father) continue; erase(dp[k]); insert(dp[k ^ 1]); if (now > ans1) { ans1 = now; ans2 = to[k]; } dfs3(to[k], pos, k); erase(dp[k ^ 1]); insert(dp[k]); } } signed main() { n = R(); unsigned long long x, y; for (unsigned long long i = 1; i < n; i++) { x = R(); y = R(); add(x, y); add(y, x); } dfs1(1, 0, 0); ans1 = now; ans2 = 1; dfs2(1, 0); dfs3(1, 0, 0); cout << ans2; }
10
CPP
#include <bits/stdc++.h> using namespace std; int lst[100005], to[200005], pre[200005], tot; int f[100005], num[200005], cur_cnt, mx, ans, cnt; long long val[200005]; map<long long, int> app; inline void add_edge(int u, int v) { to[tot] = v; pre[tot] = lst[u]; lst[u] = tot++; } inline void add(int x) { if (!num[x]) cnt++; num[x]++; } inline void del(int x) { num[x]--; if (!num[x]) cnt--; } inline int query(int x) { if (app.count(x)) return app[x]; return app[x] = cur_cnt++; } void dfs(int u, int fa = -1) { long long sum = 0; for (int i = lst[u]; ~i; i = pre[i]) { if (to[i] == fa) continue; dfs(to[i], u); sum += val[f[to[i]]]; } f[u] = query(sum); add(f[u]); } void upd(int u, int fa = -1, int w = -1) { del(f[u]); if (cnt >= mx) { mx = cnt + 1; ans = u; } long long sum = 0; for (int i = lst[u]; ~i; i = pre[i]) { if (to[i] != fa) sum += val[f[to[i]]]; } if (~w) sum += val[w]; for (int i = lst[u]; ~i; i = pre[i]) { if (to[i] == fa) continue; long long x = sum - val[f[to[i]]]; add(query(x)); upd(to[i], u, query(x)); del(query(x)); } add(f[u]); } int main() { memset(lst, -1, sizeof(lst)); int n; scanf("%d", &n); val[0] = 1; for (int i = 1; i <= n << 1; i++) val[i] = val[i - 1] * 233333; for (int i = 1; i < n; i++) { int u, v; scanf("%d%d", &u, &v); add_edge(--u, --v); add_edge(v, u); } dfs(0); upd(0); printf("%d\n", ans + 1); return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; inline long long mod(long long n, long long m) { long long ret = n % m; if (ret < 0) ret += m; return ret; } long long gcd(long long a, long long b) { return (b == 0LL ? a : gcd(b, a % b)); } long long exp(long long a, long long b, long long m) { if (b == 0LL) return 1LL; if (b == 1LL) return mod(a, m); long long k = mod(exp(a, b / 2, m), m); if (b & 1LL) { return mod(a * mod(k * k, m), m); } else return mod(k * k, m); } mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); map<pair<long long, long long>, long long> mp; const long long inf = 1e18; long long H(long long x, long long y) { if (y == 0) return 0; if (mp.count(pair<long long, long long>(x, y))) return mp[pair<long long, long long>(x, y)]; return mp[pair<long long, long long>(x, y)] = rng() % inf; } int n; const int N = 100100; vector<int> g[N]; long long Hash[N]; int res = 0; int ID = 0; long long HashCima[N]; long long ImRoot[N]; map<long long, long long> allcnt; void dfscalc(int v, int p = -1) { if (p == -1) { ImRoot[v] = Hash[v]; map<long long, long long> cnt; for (int to : g[v]) if (to != p) { cnt[Hash[to]]++; } for (int to : g[v]) if (to != p) { HashCima[to] = ImRoot[v] ^ H(Hash[to], cnt[Hash[to]]) ^ H(Hash[to], cnt[Hash[to]] - 1); } } else { map<long long, long long> cnt; cnt[HashCima[v]]++; for (int to : g[v]) if (to != p) { cnt[Hash[to]]++; } ImRoot[v] = 0; for (auto x : cnt) { ImRoot[v] ^= H(x.first, x.second); } for (int to : g[v]) if (to != p) { HashCima[to] = ImRoot[v] ^ H(Hash[to], cnt[Hash[to]]) ^ H(Hash[to], cnt[Hash[to]] - 1); } } allcnt[Hash[v]]--; allcnt[ImRoot[v]]++; if (allcnt[Hash[v]] == 0) allcnt.erase(Hash[v]); if (res < (int)allcnt.size()) { res = (int)allcnt.size(); ID = v; } allcnt[ImRoot[v]]--; if (allcnt[ImRoot[v]] == 0) allcnt.erase(ImRoot[v]); for (int to : g[v]) if (to != p) { allcnt[HashCima[to]]++; dfscalc(to, v); allcnt[HashCima[to]]--; if (allcnt[HashCima[to]] == 0) allcnt.erase(HashCima[to]); } allcnt[Hash[v]]++; } void dfspre(int v, int p = -1) { int tot = 0; for (int to : g[v]) if (to != p) { tot++; } if (tot == 0) { Hash[v] = 0; } else { map<long long, long long> cnt; for (int to : g[v]) if (to != p) { dfspre(to, v); cnt[Hash[to]]++; } for (auto x : cnt) { Hash[v] ^= H(x.first, x.second); } } allcnt[Hash[v]]++; } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; cin >> n; for (int i = 1; i < n; i++) { int a, b; cin >> a >> b; g[a].push_back(b); g[b].push_back(a); } dfspre(1, -1); dfscalc(1, -1); cout << ID << "\n"; }
10
CPP
#include <bits/stdc++.h> using namespace std; const long long mod = 1000000007; unsigned long long powmod(unsigned long long a, int b) { unsigned long long res = 1; assert(b >= 0); for (; b; b >>= 1) { if (b & 1) res = res * a; a = a * a; } return res; } const int N = 101000; vector<pair<unsigned long long, int> > h[N]; unsigned long long pw[2 * N], hv[N], h2[N], hw[N]; map<unsigned long long, int> hs; int ansv, ansp, n, u, v, sz[N]; vector<int> e[N]; void dfs(int u, int f) { for (auto v : e[u]) { if (v == f) continue; dfs(v, u); h[u].push_back(make_pair(hv[v], sz[v])); sz[u] += sz[v]; } sz[u]++; sort((h[u]).begin(), (h[u]).end()); hv[u] = 1; for (auto p : h[u]) hv[u] = hv[u] * pw[2 * p.second] + p.first; hv[u] = hv[u] * 233333 + 2; } unsigned long long inv = 2831921843306989789ull; void dfs2(int u, int f) { if (f != 0) h[u].push_back(make_pair(h2[u], n - sz[u])); sort((h[u]).begin(), (h[u]).end()); int m = ((int)(h[u]).size()); unsigned long long hs = 1; for (int i = m - 1; i >= 0; i--) hw[i] = h[u][i].first * hs, hs = hs * pw[2 * h[u][i].second]; hw[m] = 0; for (int i = m - 1; i >= 0; i--) hw[i] = hw[i] + hw[i + 1]; for (auto v : e[u]) { if (v == f) continue; int p = lower_bound((h[u]).begin(), (h[u]).end(), make_pair(hv[v], sz[v])) - h[u].begin(); h2[v] = pw[2 * (n - sz[v]) - 1] + (hw[p + 1] + (hw[0] - hw[p]) * powmod(inv, 2 * sz[v])) * 233333 + 2; } for (auto v : e[u]) { if (v == f) continue; dfs2(v, u); } } void del(unsigned long long v) { hs[v]--; if (hs[v] == 0) hs.erase(v); } void dfs3(int u, int f) { if (((int)(hs).size()) > ansv) { ansv = ((int)(hs).size()); ansp = u; } for (auto v : e[u]) { if (v == f) continue; del(hv[v]); hs[h2[v]]++; dfs3(v, u); del(h2[v]); hs[hv[v]]++; } } int main() { scanf("%d", &n); pw[0] = 1; for (int i = 1; i < 2 * n + 1; i++) pw[i] = pw[i - 1] * 233333; for (int i = 1; i < n; i++) { scanf("%d%d", &u, &v); e[u].push_back(v); e[v].push_back(u); } dfs(1, 0); dfs2(1, 0); for (int i = 2; i < n + 1; i++) hs[hv[i]]++; ansp = 1; ansv = ((int)(hs).size()); dfs3(1, 0); printf("%d\n", ansp); }
10
CPP
#include <bits/stdc++.h> using namespace std; int n; vector<int> e[100010]; bool rem[100010]; int sz[100010], mxsz[100010]; vector<int> cc; void calc_sz(int u, int prv) { cc.push_back(u); sz[u] = 1; mxsz[u] = 0; for (int v : e[u]) { if (v == prv || rem[v]) continue; calc_sz(v, u); sz[u] += sz[v]; mxsz[u] = max(mxsz[u], sz[v]); } } int find_sep(int u) { cc.clear(); calc_sz(u, 0); int nn = cc.size(); for (int v : cc) { mxsz[v] = max(mxsz[v], nn - sz[v]); if (mxsz[v] * 2 <= nn) return v; } return -1; } long long p[2 * 100010]; long long h[100010]; map<long long, int> ct; int num, best, idx; void upd(long long hh, int d) { if (ct[hh] == 0) num++; ct[hh] += d; if (ct[hh] == 0) num--; } bool hcmp(int l, int r) { return h[l] < h[r]; } void calc_hashes(int u, int prv) { if (rem[u]) return; sz[u] = 1; for (int v : e[u]) { if (v == prv) continue; calc_hashes(v, u); sz[u] += sz[v]; } sort(e[u].begin(), e[u].end(), hcmp); h[u] = 1; for (int v : e[u]) { if (v == prv) continue; h[u] = h[u] * p[2 * sz[v]] + h[v]; } h[u] = h[u] * 17 + 2; upd(h[u], 1); } void collect_hashes(int u, int prv, vector<long long> &vec) { if (rem[u]) return; vec.push_back(h[u]); for (int v : e[u]) { if (v != prv) collect_hashes(v, u, vec); } } void upd_all(vector<long long> &vec, int d) { for (long long x : vec) { upd(x, d); } } void decomp(int u) { u = find_sep(u); calc_hashes(u, 0); if (num > best) { best = num; idx = u; } rem[u] = 1; vector<long long> hh; vector<int> szs; int sufsz = 2 * sz[u] - 1; long long hsuf = h[u] - p[sufsz], hpre = 1; for (int v : e[u]) { sufsz -= 2 * sz[v]; hsuf -= h[v] * p[sufsz]; hh.push_back(hpre * p[sufsz] + hsuf); hpre = hpre * p[2 * sz[v]] + h[v]; szs.push_back(sz[v]); } vector<vector<long long> > hhh; for (int v : e[u]) { hhh.push_back(vector<long long>()); collect_hashes(v, 0, hhh.back()); } for (int i = 0; i < hh.size(); i++) { int v = e[u][i]; if (rem[v]) continue; sz[u] -= szs[i]; upd(h[u], -1); h[u] = hh[i]; upd(h[u], 1); upd_all(hhh[i], -1); decomp(v); upd_all(hhh[i], 1); sz[u] += szs[i]; } upd(h[u], -1); for (int i = 0; i < hhh.size(); i++) upd_all(hhh[i], -1); } int main() { p[0] = 1; for (int i = 1; i < 2 * 100010; i++) p[i] = p[i - 1] * 17; ios::sync_with_stdio(0); cin >> n; for (int i = 1; i < n; i++) { int u, v; cin >> u >> v; e[u].push_back(v); e[v].push_back(u); } decomp(1); cout << idx << '\n'; }
10
CPP
#include <bits/stdc++.h> using namespace std; unsigned long long hsh[100005 << 1], typ[100005], typ2[100005]; vector<int> lin[100005]; map<unsigned long long, int> cnt, subt; int hd[100005], hd2[100005], num, ansv, now, n, v, u; void init(int n) { for (int i = 0; i < 2 * (n + 1); i++) hsh[i] = ((unsigned long long)rand() << 40) + ((unsigned long long)rand() << 20) + ((unsigned long long)rand()); } void dfs1(int u, int fa) { typ[u] = hsh[0]; for (int i = 0; i < lin[u].size(); i++) { if (lin[u][i] == fa) continue; dfs1(lin[u][i], u); typ[u] += hsh[hd[lin[u][i]]]; } if (!subt.count(typ[u])) subt[typ[u]] = ++num; hd[u] = subt[typ[u]]; } void dfs2(int u, int fa) { unsigned long long h = typ[u]; if (fa != 0) h += hsh[hd2[u]]; for (int i = 0; i < lin[u].size(); i++) { if (lin[u][i] == fa) continue; typ2[lin[u][i]] = h - hsh[hd[lin[u][i]]]; if (!subt.count(typ2[lin[u][i]])) subt[typ2[lin[u][i]]] = ++num; hd2[lin[u][i]] = subt[typ2[lin[u][i]]]; } for (int i = 0; i < lin[u].size(); i++) { if (lin[u][i] == fa) continue; dfs2(lin[u][i], u); } } void dfs3(int u, int fa) { if (cnt.size() > ansv) { ansv = cnt.size(); now = u; } for (int i = 0; i < lin[u].size(); i++) { int v = lin[u][i]; if (v == fa) continue; cnt[typ[v]]--; if (cnt[typ[v]] == 0) cnt.erase(typ[v]); cnt[typ2[v]]++; dfs3(v, u); cnt[typ2[v]]--; if (cnt[typ2[v]] == 0) cnt.erase(typ2[v]); cnt[typ[v]]++; } } int main() { scanf("%d", &n); init(n); for (int i = 1; i < n; i++) { scanf("%d%d", &u, &v); lin[u].push_back(v); lin[v].push_back(u); } dfs1(1, 0); dfs2(1, 0); for (int i = 2; i <= n; i++) cnt[typ[i]]++; now = 1, ansv = cnt.size(); dfs3(1, 0); printf("%d\n", now); return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; int lst[100005], to[200005], pre[200005], tot; int f[100005], num[200005], cur_cnt, mx, ans, cnt; long long val[200005]; map<long long, int> app; inline void add_edge(int u, int v) { to[tot] = v; pre[tot] = lst[u]; lst[u] = tot++; } inline void add(int x) { if (!num[x]) cnt++; num[x]++; } inline void del(int x) { num[x]--; if (!num[x]) cnt--; } inline int query(int x) { if (app.count(x)) return app[x]; return app[x] = cur_cnt++; } void dfs(int u, int fa = -1) { long long sum = 0; for (int i = lst[u]; ~i; i = pre[i]) { if (to[i] == fa) continue; dfs(to[i], u); sum += val[f[to[i]]]; } f[u] = query(sum); add(f[u]); } void upd(int u, int fa = -1, int w = -1) { del(f[u]); if (cnt >= mx) { mx = cnt + 1; ans = u; } long long sum = 0; for (int i = lst[u]; ~i; i = pre[i]) { if (to[i] != fa) sum += val[f[to[i]]]; } if (~w) sum += val[w]; for (int i = lst[u]; ~i; i = pre[i]) { if (to[i] == fa) continue; long long x = sum - val[f[to[i]]]; add(query(x)); upd(to[i], u, query(x)); del(query(x)); } add(f[u]); } int main() { memset(lst, -1, sizeof(lst)); int n; scanf("%d", &n); val[0] = 1; for (int i = 1; i <= n << 1; i++) val[i] = val[i - 1] * 2333; for (int i = 1; i < n; i++) { int u, v; scanf("%d%d", &u, &v); add_edge(--u, --v); add_edge(v, u); } dfs(0); upd(0); printf("%d\n", ans + 1); return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; const long long mod = 1000000033, base = 79; const int maxn = 1e5 + 79; int n; vector<vector<int> > g(maxn); vector<int> siz(maxn, 0), et; vector<long long> s(maxn, 0), pwr(maxn * 2, 1); vector<map<int, long long> > h(maxn); void dfs1(int v = 0, int p = -1) { vector<pair<long long, int> > myh; et.push_back(v); siz[v] = 2; for (int i : g[v]) { if (i == p) continue; dfs1(i, v); et.push_back(v); siz[v] += siz[i]; myh.push_back({s[i], siz[i]}); } sort(myh.begin(), myh.end()); s[v] = 1; for (pair<long long, int> i : myh) { s[v] = (s[v] * pwr[i.second]) % mod; s[v] = (s[v] + i.first) % mod; } s[v] = (s[v] * base) % mod; } void dfs2(int v = 0, int p = -1, long long uph = 0) { vector<pair<long long, pair<int, int> > > myh; if (p != -1) myh.push_back({uph, {p, 2 * n - siz[v]}}); for (int i : g[v]) if (i != p) myh.push_back({s[i], {i, siz[i]}}); sort(myh.begin(), myh.end()); long long pfsum = 1, sfsum = 0; int sizsf = 1; for (int i = 0; i < myh.size(); i++) { sfsum = ((sfsum * pwr[myh[i].second.second]) % mod + myh[i].first) % mod; sizsf += myh[i].second.second; } sfsum = (sfsum * base) % mod; for (int i = 0; i < myh.size(); i++) { long long hash = myh[i].first; int vr = myh[i].second.first, cursiz = myh[i].second.second; sizsf -= cursiz; sfsum = (sfsum - (pwr[sizsf] * hash) % mod + mod) % mod; long long hash_total = ((pfsum * pwr[sizsf]) % mod + sfsum) % mod; h[v][vr] = hash_total; if (vr != p) dfs2(vr, v, hash_total); pfsum = ((pfsum * pwr[cursiz]) % mod + hash) % mod; } } int main() { ios::sync_with_stdio(false); cin.tie(0); for (int i = 1; i < pwr.size(); i++) pwr[i] = (pwr[i - 1] * base) % mod; cin >> n; for (int i = 0, u, v; i < n - 1; i++) { cin >> u >> v; g[--u].push_back(--v); g[v].push_back(u); } dfs1(); map<long long, int> f; for (int i = 0; i < n; i++) { f[s[i]]++; } pair<int, int> ans = {f.size(), 0}; dfs2(); for (int i = 1; i < et.size(); i++) { f[h[et[i]][et[i - 1]]]--; if (f[h[et[i]][et[i - 1]]] == 0) f.erase(h[et[i]][et[i - 1]]); f[h[et[i - 1]][et[i]]]++; if (f.size() > ans.first) ans = {(int)f.size(), et[i]}; } cout << ans.second + 1 << "\n"; return 0; }
10
CPP