Submission #247073

# Submission time Handle Problem Language Result Execution time Memory
247073 2020-07-10T22:36:26 Z MKopchev Inspection (POI11_ins) C++14
100 / 100
1606 ms 127180 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];

int subtree[nmax];
int h[nmax];

void dfs_main(int node,int par,int d)
{
    h[node]=d;
    subtree[node]=1;

    for(auto k:adj[node])
        if(k!=par)
        {
            dfs_main(k,node,d+1);
            subtree[node]+=subtree[k];
        }
}
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 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);
    }

    dfs_main(1,0,0);

    for(int i=1;i<=n;i++)
    {
        int MX=0;
        for(auto k:adj[i])
            if(h[i]<h[k])MX=max(MX,subtree[k]);
            else MX=max(MX,n-subtree[i]);

        if(2*MX>n)printf("-1\n");
        else printf("%lld\n",ask(i));
    }

    return 0;
}

Compilation message

ins.cpp: In function 'int main()':
ins.cpp:58:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%i",&n);
     ~~~~~^~~~~~~~~
ins.cpp:64:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%i%i",&u,&v);
         ~~~~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 18 ms 23808 KB Output is correct
2 Correct 18 ms 23808 KB Output is correct
3 Correct 20 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 18 ms 23936 KB Output is correct
# Verdict Execution time Memory 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 18 ms 23808 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 23936 KB Output is correct
2 Correct 19 ms 23936 KB Output is correct
3 Correct 19 ms 23936 KB Output is correct
4 Correct 19 ms 23936 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 29 ms 24832 KB Output is correct
2 Correct 30 ms 25472 KB Output is correct
3 Correct 30 ms 25856 KB Output is correct
4 Correct 30 ms 25208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 44 ms 25848 KB Output is correct
2 Correct 42 ms 27128 KB Output is correct
3 Correct 42 ms 27768 KB Output is correct
4 Correct 42 ms 26488 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 88 ms 28920 KB Output is correct
2 Correct 94 ms 31992 KB Output is correct
3 Correct 98 ms 34040 KB Output is correct
4 Correct 80 ms 30200 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 729 ms 49528 KB Output is correct
2 Correct 765 ms 65784 KB Output is correct
3 Correct 705 ms 75384 KB Output is correct
4 Correct 536 ms 56032 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1606 ms 75020 KB Output is correct
2 Correct 1534 ms 107804 KB Output is correct
3 Correct 1548 ms 127180 KB Output is correct
4 Correct 1203 ms 88324 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1580 ms 75060 KB Output is correct
2 Correct 1504 ms 107768 KB Output is correct
3 Correct 1535 ms 127116 KB Output is correct
4 Correct 1196 ms 88428 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1568 ms 75264 KB Output is correct
2 Correct 1502 ms 107840 KB Output is correct
3 Correct 1524 ms 127168 KB Output is correct
4 Correct 1193 ms 88316 KB Output is correct