Submission #2667686


Source Code Expand

#include<bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int,int>ii;
typedef pair<int,ii>iii;
typedef vector<int>vi;
vector<vi>adjlist;
vector<iii>edge;
const int N=1e5+5;
int ans[N],lazy[N];
int pa[N],sz[N];
void unionset(int x,int y,int w)
{
    x=pa[x];
    y=pa[y];
    if(sz[x]<sz[y]){
        lazy[y]+=sz[x]*w;
        for(int i=0;i<adjlist[x].size();++i){
            int v=adjlist[x][i];
            pa[v]=y;
            adjlist[y].push_back(v);
            ans[v]+=lazy[x];
            ans[v]+=sz[y]*w;
            ans[v]-=lazy[y];
        }
        sz[y]+=sz[x];
    }
    else{
        lazy[x]+=sz[y]*w;//chu y ko phai cong trc vi co the no da qua may doi chua tru
        for(int i=0;i<adjlist[y].size();++i){
            int v=adjlist[y][i];
            pa[v]=x;
            adjlist[x].push_back(v);
            ans[v]+=lazy[y];
            ans[v]+=sz[x]*w;
            ans[v]-=lazy[x];
        }
        sz[x]+=sz[y];
    }
}
signed main()
{
    int n;
    scanf("%lld",&n);
    adjlist.assign(n+1,vi());
    for(int i=1;i<=n;++i){
        adjlist[i].push_back(i);
        pa[i]=i;
        sz[i]=1;
    }
    for(int i=1;i<n;++i){
        int u,v,w;
        scanf("%lld%lld%d",&u,&v,&w);
        edge.push_back(iii(w,ii(u,v)));
    }
    sort(edge.begin(),edge.end());
    reverse(edge.begin(),edge.end());
    for(int i=0;i<edge.size();++i){
        unionset(edge[i].second.first,edge[i].second.second,edge[i].first);
    }
    int root=0;
    for(int i=1;i<=n;++i)if(pa[i]==i)root=i;
    for(int i=0;i<adjlist[root].size();i++)ans[adjlist[root][i]]+=lazy[root];
    for(int i=1;i<=n;++i)printf("%lld\n",ans[i]);
}

Submission Info

Submission Time
Task E - Black Cats Deployment
User vjudge3
Language C++14 (GCC 5.4.1)
Score 800
Code Size 1653 Byte
Status AC
Exec Time 89 ms
Memory 20592 KB

Compile Error

./Main.cpp: In function ‘int main()’:
./Main.cpp:53:36: warning: format ‘%d’ expects argument of type ‘int*’, but argument 4 has type ‘long long int*’ [-Wformat=]
         scanf("%lld%lld%d",&u,&v,&w);
                                    ^
./Main.cpp:44:21: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
     scanf("%lld",&n);
                     ^
./Main.cpp:53:37: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
         scanf("%lld%lld%d",&u,&v,&w);
                                     ^

Judge Result

Set Name Sample Subtask1 Subtask2 All
Score / Max Score 0 / 0 200 / 200 200 / 200 400 / 400
Status
AC × 3
AC × 10
AC × 9
AC × 32
Set Name Test Cases
Sample 00_example_01.txt, 00_example_02.txt, 00_example_03.txt
Subtask1 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
Subtask2 00_example_02.txt, s1_07.txt, s2_08.txt, s2_09.txt, s2_10.txt, s2_11.txt, s2_12.txt, s2_13.txt, s2_14.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, s2_08.txt, s2_09.txt, s2_10.txt, s2_11.txt, s2_12.txt, s2_13.txt, s2_14.txt, s3_15.txt, s3_16.txt, s3_17.txt, s3_18.txt, s3_19.txt, s3_20.txt, s3_21.txt, s3_22.txt, s3_23.txt, s3_24.txt, s3_25.txt, s3_26.txt, s3_27.txt, s3_28.txt, s3_29.txt
Case Name Status Exec Time Memory
00_example_01.txt AC 1 ms 256 KB
00_example_02.txt AC 1 ms 256 KB
00_example_03.txt AC 1 ms 256 KB
s1_01.txt AC 2 ms 384 KB
s1_02.txt AC 2 ms 384 KB
s1_03.txt AC 1 ms 384 KB
s1_04.txt AC 2 ms 384 KB
s1_05.txt AC 2 ms 384 KB
s1_06.txt AC 2 ms 384 KB
s1_07.txt AC 1 ms 256 KB
s2_08.txt AC 61 ms 13552 KB
s2_09.txt AC 31 ms 7024 KB
s2_10.txt AC 17 ms 3960 KB
s2_11.txt AC 71 ms 14832 KB
s2_12.txt AC 71 ms 16112 KB
s2_13.txt AC 82 ms 19568 KB
s2_14.txt AC 80 ms 20336 KB
s3_15.txt AC 68 ms 14064 KB
s3_16.txt AC 33 ms 7280 KB
s3_17.txt AC 18 ms 4088 KB
s3_18.txt AC 32 ms 7028 KB
s3_19.txt AC 80 ms 15856 KB
s3_20.txt AC 83 ms 16752 KB
s3_21.txt AC 81 ms 15600 KB
s3_22.txt AC 79 ms 15472 KB
s3_23.txt AC 89 ms 20592 KB
s3_24.txt AC 86 ms 20080 KB
s3_25.txt AC 64 ms 13680 KB
s3_26.txt AC 79 ms 19440 KB
s3_27.txt AC 66 ms 13808 KB
s3_28.txt AC 81 ms 18800 KB
s3_29.txt AC 64 ms 13936 KB