結果
問題 | No.1418 Sum of Sum of Subtree Size |
ユーザー | tada721 |
提出日時 | 2021-02-05 17:03:39 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 140 ms / 2,000 ms |
コード長 | 7,621 bytes |
コンパイル時間 | 2,175 ms |
コンパイル使用メモリ | 108,332 KB |
実行使用メモリ | 28,416 KB |
最終ジャッジ日時 | 2024-07-02 07:13:58 |
合計ジャッジ時間 | 4,808 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 6 ms
8,576 KB |
testcase_01 | AC | 6 ms
8,320 KB |
testcase_02 | AC | 6 ms
8,576 KB |
testcase_03 | AC | 128 ms
21,760 KB |
testcase_04 | AC | 125 ms
21,632 KB |
testcase_05 | AC | 125 ms
21,888 KB |
testcase_06 | AC | 140 ms
21,760 KB |
testcase_07 | AC | 140 ms
21,760 KB |
testcase_08 | AC | 82 ms
17,408 KB |
testcase_09 | AC | 39 ms
12,544 KB |
testcase_10 | AC | 42 ms
13,056 KB |
testcase_11 | AC | 24 ms
10,880 KB |
testcase_12 | AC | 64 ms
15,360 KB |
testcase_13 | AC | 79 ms
16,768 KB |
testcase_14 | AC | 81 ms
17,408 KB |
testcase_15 | AC | 60 ms
14,976 KB |
testcase_16 | AC | 12 ms
9,216 KB |
testcase_17 | AC | 12 ms
9,344 KB |
testcase_18 | AC | 115 ms
20,480 KB |
testcase_19 | AC | 22 ms
10,624 KB |
testcase_20 | AC | 9 ms
8,832 KB |
testcase_21 | AC | 57 ms
14,592 KB |
testcase_22 | AC | 50 ms
13,696 KB |
testcase_23 | AC | 10 ms
8,832 KB |
testcase_24 | AC | 9 ms
8,960 KB |
testcase_25 | AC | 8 ms
8,832 KB |
testcase_26 | AC | 9 ms
8,960 KB |
testcase_27 | AC | 7 ms
8,576 KB |
testcase_28 | AC | 7 ms
8,576 KB |
testcase_29 | AC | 10 ms
9,088 KB |
testcase_30 | AC | 10 ms
9,088 KB |
testcase_31 | AC | 8 ms
8,832 KB |
testcase_32 | AC | 9 ms
8,832 KB |
testcase_33 | AC | 28 ms
12,800 KB |
testcase_34 | AC | 120 ms
28,416 KB |
testcase_35 | AC | 53 ms
16,768 KB |
testcase_36 | AC | 15 ms
9,984 KB |
testcase_37 | AC | 83 ms
21,328 KB |
testcase_38 | AC | 77 ms
20,400 KB |
testcase_39 | AC | 6 ms
8,448 KB |
testcase_40 | AC | 6 ms
8,448 KB |
testcase_41 | AC | 6 ms
8,576 KB |
testcase_42 | AC | 6 ms
8,576 KB |
testcase_43 | AC | 6 ms
8,576 KB |
コンパイルメッセージ
main.cpp: In function 'int main()': main.cpp:393:24: warning: division by zero [-Wdiv-by-zero] 393 | cout<<n/(n-n)<<endl; | ~^~~~~~ main.cpp: In function 'long long int keta(long long int)': main.cpp:42:1: warning: control reaches end of non-void function [-Wreturn-type] 42 | } | ^ main.cpp: In function 'long long int gcd(long long int, long long int)': main.cpp:56:1: warning: control reaches end of non-void function [-Wreturn-type] 56 | } | ^ main.cpp: In function 'long long int lcm(long long int, long long int)': main.cpp:69:1: warning: control reaches end of non-void function [-Wreturn-type] 69 | } | ^ main.cpp: In function 'bool ok()': main.cpp:386:1: warning: control reaches end of non-void function [-Wreturn-type] 386 | } | ^
ソースコード
#include<iostream> #include<algorithm> #include<cmath> #include<map> #include<stdio.h> #include<vector> #include<queue> #include<math.h> #include<deque> #include<set> #include<bitset> #include<ctime> #include<random> using namespace std; #define double long double #define int long long #define rep(s,i,n) for(int i=s;i<n;i++) #define c(n) cout<<n<<endl; #define ic(n) int n;cin>>n; #define sc(s) string s;cin>>s; #define dc(d) double d;cin>>d; #define mod 1000000007 #define inf 1000000000000000007 #define f first #define s second #define mini(c,a,b) *min_element(c+a,c+b) #define maxi(c,a,b) *max_element(c+a,c+b) #define pi 3.141592653589793238462643383279 #define e_ 2.718281828459045235360287471352 #define P pair<int,int> #define upp(a,n,x) upper_bound(a,a+n,x)-a; #define low(a,n,x) lower_bound(a,a+n,x)-a; #define pb push_back //printf("%.12Lf\n",); int keta(int x) { rep(0, i, 30) { if (x < 10) { return i + 1; } x = x / 10; } } int gcd(int x, int y) { if (x == 0 || y == 0)return x + y; int aa = x, bb = y; rep(0, i, 1000) { aa = aa % bb; if (aa == 0) { return bb; } bb = bb % aa; if (bb == 0) { return aa; } } } int lcm(int x, int y) { int aa = x, bb = y; rep(0, i, 1000) { aa = aa % bb; if (aa == 0) { return x / bb * y; } bb = bb % aa; if (bb == 0) { return x / aa * y; } } } int integer(double d){ return long(d); } int distance(double a,double b,double c,double d){ return sqrt((b-a)*(b-a)+(c-d)*(c-d)); } bool p(int x) { if (x == 1)return false; rep(2, i, sqrt(x) + 1) { if (x % i == 0 && x != i) { return false; } } return true; } int max(int a, int b) { if (a >= b)return a; else return b; } string maxst(string s, string t) { int n = s.size(); int m = t.size(); if (n > m)return s; else if (n < m)return t; else { rep(0, i, n) { if (s[i] > t[i])return s; if (s[i] < t[i])return t; } return s; } } int min(int a, int b) { if (a >= b)return b; else return a; } string string_reverse(string s){ int n=s.size(); string t; rep(0,i,n)t+=s[n-i-1]; return t; } int n2[41]; int nis[41]; int nia[41]; int mody[41]; int nn; int com(int n, int y) { int ni = 1; for (int i = 0;i < 41;i++) { n2[i] = ni; ni *= 2; } int bunsi = 1, bunbo = 1; rep(0, i, y)bunsi = (bunsi * (n - i)) % mod; rep(0, i, y)bunbo = (bunbo * (i + 1)) % mod; mody[0] = bunbo; rep(1, i, 41) { bunbo = (bunbo * bunbo) % mod; mody[i] = bunbo; } rep(0, i, 41)nis[i] = 0; nn = mod - 2; for (int i = 40;i >= 0;i -= 1) { if (nn > n2[i]) { nis[i]++; nn -= n2[i]; } } nis[0]++; rep(0, i, 41) { if (nis[i] == 1) { bunsi = (bunsi * mody[i]) % mod; } } return bunsi; } int newcom(int n,int y){ int bunsi = 1, bunbo = 1; rep(0, i, y){ bunsi = (bunsi * (n - i)) ; bunbo = (bunbo * (i + 1)) ; int k=gcd(bunsi,bunbo); bunsi/=k; bunbo/=k; } return bunsi/bunbo; } int gyakugen(int n, int y) { int ni = 1; for (int i = 0;i < 41;i++) { n2[i] = ni; ni *= 2; } mody[0] = y; rep(1, i, 41) { y = (y * y) % mod; mody[i] = y; } rep(0, i, 41)nis[i] = 0; nn = mod - 2; for (int i = 40;i >= 0;i -= 1) { if (nn > n2[i]) { nis[i]++; nn -= n2[i]; } } nis[0]++; rep(0, i, 41) { if (nis[i] == 1) { n = (n * mody[i]) % mod; } } return n; } int yakuwa(int n) { int sum = 0; rep(1, i, sqrt(n + 1)) { if (n % i == 0)sum += i + n / i; if (i * i == n)sum -= i; } return sum; } int poow(int y, int n) { if (n == 0)return 1; n -= 1; int ni = 1; for (int i = 0;i < 41;i++) { n2[i] = ni; ni *= 2; } int yy = y; mody[0] = yy; rep(1, i, 41) { yy = (yy * yy) % mod; mody[i] = yy; } rep(0, i, 41)nis[i] = 0; nn = n; for (int i = 40;i >= 0;i -= 1) { if (nn >= n2[i]) { nis[i]++; nn -= n2[i]; } } rep(0, i, 41) { if (nis[i] == 1) { y = (y * mody[i]) % mod; } } return y; } int minpow(int x, int y) { int sum = 1; rep(0, i, y)sum *= x; return sum; } int ketawa(int x, int sinsuu) { int sum = 0; rep(0, i, 100)sum += (x % poow(sinsuu, i + 1)) / (poow(sinsuu, i)); return sum; } int sankaku(int a) { if(a%2==0) return a /2*(a+1); else return (a+1)/2*a; } int sames(int a[1111111], int n) { int ans = 0; rep(0, i, n) { if (a[i] == a[i + 1]) { int j = i; while (a[j + 1] == a[i] && j <= n - 2)j++; ans += sankaku(j - i); i = j; } } return ans; } using Graph = vector<vector<int>>; int oya[214514]; int depth[214514]; int subtreesize[214514]; void dfs(const Graph& G, int v, int p, int d) { depth[v] = d; oya[v] = p; for (auto nv : G[v]) { if (nv == p) continue; // nv が親 p だったらダメ dfs(G, nv, v, d + 1); // d を 1 増やして子ノードへ } subtreesize[v]=1; for(auto c:G[v]){ if(c==p)continue; subtreesize[v]+=subtreesize[c]; } } /*int H=10,W=10; char field[10][10]; char memo[10][10]; void dfs(int h, int w) { memo[h][w] = 'x'; // 八方向を探索 for (int dh = -1; dh <= 1; ++dh) { for (int dw = -1; dw <= 1; ++dw) { if(abs(0-dh)+abs(0-dw)==2)continue; int nh = h + dh, nw = w + dw; // 場外アウトしたり、0 だったりはスルー if (nh < 0 || nh >= H || nw < 0 || nw >= W) continue; if (memo[nh][nw] == 'x') continue; // 再帰的に探索 dfs(nh, nw); } } }*/ int medi(int a,int b,int c){ return a+b+c-max({a,b,c})-min({a,b,c}); } int XOR(int a, int b) { if (a == 0 || b == 0) { return a + b; } int ni = 1; rep(0, i, 41) { n2[i] = ni; ni *= 2; } rep(0, i, 41)nis[i] = 0; for (int i = 40;i >= 0;i -= 1) { if (a >= n2[i]) { nis[i]++; a -= n2[i]; } if (b >= n2[i]) { nis[i]++; b -= n2[i]; } } int sum = 0; rep(0, i, 41)sum += (nis[i] % 2 * n2[i]); return sum; } //int ma[1024577][21]; //for(int bit=0;bit<(1<<n);bit++)rep(0,i,n)if(bit&(1<<i))ma[bit][i]=1; struct UnionFind { vector<int> par; // par[i]:iの親の番号 (例) par[3] = 2 : 3の親が2 UnionFind(int N) : par(N) { //最初は全てが根であるとして初期化 for (int i = 0; i < N; i++) par[i] = i; } int root(int x) { // データxが属する木の根を再帰で得る:root(x) = {xの木の根} if (par[x] == x) return x; return par[x] = root(par[x]); } void unite(int x, int y) { // xとyの木を併合 int rx = root(x); //xの根をrx int ry = root(y); //yの根をry if (rx == ry) return; //xとyの根が同じ(=同じ木にある)時はそのまま par[rx] = ry; //xとyの根が同じでない(=同じ木にない)時:xの根rxをyの根ryにつける } bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す int rx = root(x); int ry = root(y); return rx == ry; } }; int n; int a[214514],b[214514]; vector<int> ve[214514]; int solve1(){ Graph G(n); rep(0,i,n-1){ G[a[i]].pb(b[i]); G[b[i]].pb(a[i]); } rep(0,i,n){ if(G[i].size()==1){ dfs(G,i,-1,0); break; } } rep(0,i,n-1){ if(depth[a[i]]>depth[b[i]]){ ve[a[i]].pb(n-subtreesize[a[i]]); ve[b[i]].pb(subtreesize[a[i]]); } else{ ve[b[i]].pb(n-subtreesize[b[i]]); ve[a[i]].pb(subtreesize[b[i]]); } } int ans=0; rep(0,i,n){ ans+=2*n-1; int sum=0; rep(0,j,ve[i].size())sum+=ve[i][j]; rep(0,j,ve[i].size())ans+=(sum-ve[i][j])*ve[i][j]; ve[i].clear(); } G.clear(); return ans; } bool ok(){ if(n<2||n>100000)return false; UnionFind d(n+1); rep(0,i,n-1){ if(a[i]<=0||a[i]>n||b[i]<=0||b[i]>n||a[i]==b[i]){ return false; } d.unite(a[i],b[i]); } rep(1,i,n)if(!d.same(i,i+1))return false; } signed main(){ cin>>n; rep(0,i,n-1){ cin>>a[i]>>b[i]; } if(!ok){ cout<<n/(n-n)<<endl; } rep(0,i,n-1){ a[i]-=1; b[i]-=1; } c(solve1()) }