Submission #2095962


Source Code Expand

#include "bits/stdc++.h"
using namespace std;
const long long mod = 1000000007;
const int maxn = 100100;
vector<int> tr[maxn];
int a[maxn], b[maxn], c[maxn], sz[maxn];
long long s[maxn], dw[maxn], res[maxn];
int nsz[maxn], par[maxn];
void in(int cur){
    nsz[cur] = 1;
    for(int id : tr[cur]){
        int nxt = cur ^ c[id];
        if(nxt == par[cur]) continue;
        par[nxt] = cur;
        in(nxt);
        nsz[cur] += nsz[nxt];
    }
}
int n;
void solve(int cur, int pr = -1){
    sz[cur] = 1;
    dw[cur] = 0;
    for(int id : tr[cur]){
        int nxt = cur ^ c[id];
        if(nxt == pr) continue;
        solve(nxt, cur);
        sz[cur] += sz[nxt];
    }
    for(int id : tr[cur]){
        int nxt = cur ^ c[id];
        if(nxt == pr) continue;
        int den = ((n - sz[nxt] + 1) - 1 - sz[nxt]);
        if(den == 0){
            res[id] = (s[cur] - dw[nxt]) / (sz[nxt]);
        } else {
            res[id] = (s[nxt] - s[cur]) / den;
        }
        dw[cur] += dw[nxt] + (sz[nxt]) * res[id];
    }
}
int main(){
    scanf("%d", &n);
    for(int e = 0; e < n - 1; e++){
        scanf("%d %d", a + e, b + e); c[e] = a[e] ^ b[e];
        tr[a[e]].push_back(e);
        tr[b[e]].push_back(e);
    }
    for(int e = 1; e <= n; e++) scanf("%lld", s + e);
    in(1);
    vector<int> bad_edge;
    for(int e = 0; e < n - 1; e++){
        if(par[a[e]] == b[e]){
            if(n - nsz[a[e]] == nsz[a[e]]) bad_edge.push_back(e);
        } else {
            if(n - nsz[b[e]] == nsz[b[e]]) bad_edge.push_back(e);
        }
    }
    if(bad_edge.size()){
        assert(bad_edge.size() == 1);
        int u = a[bad_edge[0]], v = b[bad_edge[0]];
        solve(u, v);
        solve(v, u);
        long long s0 = s[u] - dw[u] - dw[v];
        long long s1 = s[v] - dw[u] - dw[v];
        res[bad_edge[0]] = (s0 / sz[v]);
    } else
        solve(1);

    for(int e = 0; e < n - 1; e++) cout << res[e] << endl;
	return 0;
}

Submission Info

Submission Time
Task D - Ancient Tree Record
User FMota
Language C++14 (GCC 5.4.1)
Score 800
Code Size 1999 Byte
Status AC
Exec Time 226 ms
Memory 18816 KB

Compile Error

./Main.cpp: In function ‘int main()’:
./Main.cpp:42:20: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &n);
                    ^
./Main.cpp:44:37: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d %d", a + e, b + e); c[e] = a[e] ^ b[e];
                                     ^
./Main.cpp:48:53: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
     for(int e = 1; e <= n; e++) scanf("%lld", s + e);
                                                     ^

Judge Result

Set Name Sample Subtask1 Subtask2 All
Score / Max Score 0 / 0 300 / 300 200 / 200 300 / 300
Status
AC × 3
AC × 14
AC × 11
AC × 48
Set Name Test Cases
Sample 00_example_01.txt, 00_example_02.txt, 00_example_03.txt
Subtask1 00_example_01.txt, s1_01.txt, s1_02.txt, s1_03.txt, s1_04.txt, s1_05.txt, s1_06.txt, s1_07.txt, s1_08.txt, s1_09.txt, s1_10.txt, s1_11.txt, s1_12.txt, s1_13.txt
Subtask2 00_example_02.txt, s2_14.txt, s2_15.txt, s2_16.txt, s2_17.txt, s2_18.txt, s2_19.txt, s2_20.txt, s2_21.txt, s2_22.txt, s2_23.txt
All 00_example_01.txt, 00_example_02.txt, 00_example_03.txt, s1_01.txt, s1_02.txt, s1_03.txt, s1_04.txt, s1_05.txt, s1_06.txt, s1_07.txt, s1_08.txt, s1_09.txt, s1_10.txt, s1_11.txt, s1_12.txt, s1_13.txt, s2_14.txt, s2_15.txt, s2_16.txt, s2_17.txt, s2_18.txt, s2_19.txt, s2_20.txt, s2_21.txt, s2_22.txt, s2_23.txt, s3_24.txt, s3_25.txt, s3_26.txt, s3_27.txt, s3_28.txt, s3_29.txt, s3_30.txt, s3_31.txt, s3_32.txt, s3_33.txt, s3_34.txt, s3_35.txt, s3_36.txt, s3_37.txt, s3_38.txt, s3_39.txt, s3_40.txt, s3_41.txt, s3_42.txt, s3_43.txt, s3_44.txt, s3_45.txt
Case Name Status Exec Time Memory
00_example_01.txt AC 3 ms 5248 KB
00_example_02.txt AC 3 ms 5248 KB
00_example_03.txt AC 3 ms 5248 KB
s1_01.txt AC 120 ms 11264 KB
s1_02.txt AC 170 ms 16128 KB
s1_03.txt AC 9 ms 5632 KB
s1_04.txt AC 142 ms 14336 KB
s1_05.txt AC 24 ms 6656 KB
s1_06.txt AC 208 ms 18816 KB
s1_07.txt AC 211 ms 15616 KB
s1_08.txt AC 215 ms 18816 KB
s1_09.txt AC 3 ms 5248 KB
s1_10.txt AC 3 ms 5248 KB
s1_11.txt AC 3 ms 5248 KB
s1_12.txt AC 3 ms 5248 KB
s1_13.txt AC 3 ms 5248 KB
s2_14.txt AC 110 ms 8956 KB
s2_15.txt AC 157 ms 10360 KB
s2_16.txt AC 196 ms 11512 KB
s2_17.txt AC 196 ms 11512 KB
s2_18.txt AC 195 ms 11512 KB
s2_19.txt AC 194 ms 11512 KB
s2_20.txt AC 3 ms 5248 KB
s2_21.txt AC 3 ms 5248 KB
s2_22.txt AC 3 ms 5248 KB
s2_23.txt AC 3 ms 5248 KB
s3_24.txt AC 120 ms 8832 KB
s3_25.txt AC 174 ms 9984 KB
s3_26.txt AC 10 ms 5504 KB
s3_27.txt AC 143 ms 9216 KB
s3_28.txt AC 24 ms 5888 KB
s3_29.txt AC 221 ms 11136 KB
s3_30.txt AC 216 ms 11264 KB
s3_31.txt AC 218 ms 11136 KB
s3_32.txt AC 226 ms 14848 KB
s3_33.txt AC 216 ms 17664 KB
s3_34.txt AC 204 ms 11512 KB
s3_35.txt AC 200 ms 11512 KB
s3_36.txt AC 3 ms 5248 KB
s3_37.txt AC 3 ms 5248 KB
s3_38.txt AC 3 ms 5248 KB
s3_39.txt AC 3 ms 5248 KB
s3_40.txt AC 120 ms 8832 KB
s3_41.txt AC 167 ms 9984 KB
s3_42.txt AC 10 ms 5504 KB
s3_43.txt AC 217 ms 11136 KB
s3_44.txt AC 211 ms 11264 KB
s3_45.txt AC 215 ms 11136 KB