답안 #247071

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
247071 2020-07-10T22:31:10 Z MKopchev Inspection (POI11_ins) C++14
30 / 100
4000 ms 64180 KB
#include<bits/stdc++.h>
using namespace std;

const int nmax=1e6+42;

int n;
vector<int> adj[nmax];

long long sum=0;

int SZ[nmax];

int mx_depth[nmax];

void dfs(int node,int par,int d)
{
    sum+=2*d;
    mx_depth[node]=d;

    SZ[node]=1;

    for(auto k:adj[node])
        if(k!=par)
        {
            dfs(k,node,d+1);
            SZ[node]+=SZ[k];
            mx_depth[node]=max(mx_depth[node],mx_depth[k]);
        }
}
long long ask(int node)
{
    sum=0;

    dfs(node,0,0);

    for(auto w:adj[node])
        if(SZ[w]*2>SZ[node])return -1;

    for(auto w:adj[node])
        if(SZ[w]*2==SZ[node])return sum-mx_depth[w];

    return sum-mx_depth[node];
}
int main()
{
    scanf("%i",&n);

    int u,v;

    for(int i=1;i<n;i++)
    {
        scanf("%i%i",&u,&v);
        adj[u].push_back(v);
        adj[v].push_back(u);
    }

    for(int i=1;i<=n;i++)
        printf("%lld\n",ask(i));

    return 0;
}

Compilation message

ins.cpp: In function 'int main()':
ins.cpp:46:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%i",&n);
     ~~~~~^~~~~~~~~
ins.cpp:52:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%i%i",&u,&v);
         ~~~~~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 23808 KB Output is correct
2 Correct 18 ms 23936 KB Output is correct
3 Correct 18 ms 23808 KB Output is correct
4 Correct 18 ms 23808 KB Output is correct
5 Correct 19 ms 23808 KB Output is correct
6 Correct 19 ms 23808 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 23808 KB Output is correct
2 Correct 18 ms 23808 KB Output is correct
3 Correct 18 ms 23808 KB Output is correct
4 Correct 19 ms 23808 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 91 ms 23936 KB Output is correct
2 Correct 80 ms 23936 KB Output is correct
3 Correct 77 ms 24036 KB Output is correct
4 Correct 76 ms 23936 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 4064 ms 24844 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 4075 ms 25592 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 4078 ms 27768 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 4080 ms 44024 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 4075 ms 63964 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 4054 ms 64180 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 4083 ms 64096 KB Time limit exceeded
2 Halted 0 ms 0 KB -