Submission #202129

# Submission time Handle Problem Language Result Execution time Memory
202129 2020-02-14T01:35:48 Z gs18115 Unique Cities (JOI19_ho_t5) C++14
100 / 100
911 ms 45176 KB
#include<iostream>
#include<vector>
#include<algorithm>
#define ep emplace
#define eb emplace_back
#define fi first
#define se second
#define all(x) (x).begin(),(x).end()
#define semicolon ;
#define ryan bear
using namespace std;
typedef long long ll;
typedef pair<int,int>pi;
typedef pair<ll,ll>pl;
const int inf=1e9+7;
const ll INF=1e18+7;
struct seg
{
    int c[1000010],sm[1000010],t[1000010];
    seg(){}
    inline void init()
    {
        fill(c,c+1000010,0);
        fill(sm,sm+1000010,0);
        fill(t,t+1000010,0);
    }
    inline void set(int n)
    {
        t[n]=c[n]>0?sm[n]:(n<400005?t[n*2]+t[n*2+1]:0);
        return;
    }
    void set(int n,int s,int e,int x,int p)
    {
        if(s==e)
        {
            sm[n]=p;
            set(n);
            return;
        }
        int m=(s+e)/2;
        if(x>m)
            set(n*2+1,m+1,e,x,p);
        else
            set(n*2,s,m,x,p);
        sm[n]=sm[n*2]+sm[n*2+1];
        set(n);
        return;
    }
    bool get(int n,int s,int e,int x)
    {
        if(c[n]>0)
            return 1;
        if(s==e)
            return 0;
        int m=(s+e)/2;
        if(x>m)
            return get(n*2+1,m+1,e,x);
        return get(n*2,s,m,x);
    }
    void si(int n,int s,int e,int S,int E,int p)
    {
        if(s>E||S>e)
            return;
        if(S<=s&&e<=E)
        {
            c[n]+=p;
            set(n);
            return;
        }
        int m=(s+e)/2;
        si(n*2,s,m,S,E,p);
        si(n*2+1,m+1,e,S,E,p);
        set(n);
        return;
    }
    inline int sq()
    {
        return t[1];
    }
}st;
vector<int>adj[250010];
int dp[250010],dp2[250010];
int fd[250010],sd[250010];
bool ch[250010];
int pos[250010];
int n,m;
void dfs(int x,int p)
{
    ch[x]=dp[p]+1>dp[x];
    dp[x]=dp[p]+1;
    for(int&t:adj[x])
        if(t!=p)
            dfs(t,x);
    fd[x]=sd[x]=0;
    for(int&t:adj[x])
    {
        if(t==p)
            continue;
        if(fd[x]<dp2[t])
            sd[x]=fd[x],fd[x]=dp2[t];
        else if(sd[x]<dp2[t])
            sd[x]=dp2[t];
    }
    dp2[x]=fd[x]+1;
    return;
}
int ans[250010],c[250010];
int c2;
void dfs2(int x,int p)
{
    if(ch[x])
    {
        int up=max(1,dp[x]-fd[x]);
        if(up<dp[x])
            st.si(1,1,n,up,dp[x]-1,1);
        ans[x]=c2-st.sq();
        if(up<dp[x])
            st.si(1,1,n,up,dp[x]-1,-1);
    }
    for(int&t:adj[x])
    {
        if(t==p)
            continue;
        int up=max(1,dp[x]-(dp2[t]==fd[x]?sd[x]:fd[x]));
        if(up<dp[x])
            st.si(1,1,n,up,dp[x]-1,1);
        int pp=pos[c[x]];
        bool cn=0;
        if(pp==0||st.get(1,1,n,pp))
            pos[c[x]]=dp[x],cn=1,st.set(1,1,n,dp[x],1),c2++;
        dfs2(t,x);
        if(cn)
            pos[c[x]]=pp,st.set(1,1,n,dp[x],0),c2--;
        if(up<dp[x])
            st.si(1,1,n,up,dp[x]-1,-1);
    }
    return;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cin>>n>>m;
    for(int i=1;i<n;i++)
    {
        int u,v;
        cin>>u>>v;
        adj[u].eb(v);
        adj[v].eb(u);
    }
    for(int i=0;i++<n;)
        cin>>c[i];
    dfs(1,0);
    int l1=max_element(dp+1,dp+n+1)-dp;
    dfs(l1,0);
    for(int i=0;i++<n;)
        ch[i]=1;
    dfs2(l1,0);
    int l2=max_element(dp+1,dp+n+1)-dp;
    for(int i=0;i++<n;)
        ch[i]=0;
    dfs(l2,0);
    dfs2(l2,0);
    for(int i=0;i++<n;)
        cout<<ans[i]<<'\n';
    cout.flush();
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 8 ms 6264 KB Output is correct
2 Correct 11 ms 6392 KB Output is correct
3 Correct 10 ms 6520 KB Output is correct
4 Correct 11 ms 6520 KB Output is correct
5 Correct 12 ms 6424 KB Output is correct
6 Correct 11 ms 6652 KB Output is correct
7 Correct 11 ms 6520 KB Output is correct
8 Correct 12 ms 6392 KB Output is correct
9 Correct 13 ms 6392 KB Output is correct
10 Correct 11 ms 6392 KB Output is correct
11 Correct 12 ms 6392 KB Output is correct
12 Correct 11 ms 6392 KB Output is correct
13 Correct 11 ms 6648 KB Output is correct
14 Correct 11 ms 6520 KB Output is correct
15 Correct 11 ms 6520 KB Output is correct
16 Correct 11 ms 6392 KB Output is correct
17 Correct 11 ms 6520 KB Output is correct
18 Correct 11 ms 6520 KB Output is correct
19 Correct 12 ms 6392 KB Output is correct
20 Correct 11 ms 6648 KB Output is correct
21 Correct 12 ms 6520 KB Output is correct
22 Correct 11 ms 6392 KB Output is correct
23 Correct 11 ms 6392 KB Output is correct
24 Correct 12 ms 6396 KB Output is correct
25 Correct 11 ms 6392 KB Output is correct
26 Correct 10 ms 6392 KB Output is correct
27 Correct 12 ms 6520 KB Output is correct
28 Correct 11 ms 6520 KB Output is correct
29 Correct 12 ms 6520 KB Output is correct
30 Correct 10 ms 6392 KB Output is correct
31 Correct 13 ms 6520 KB Output is correct
32 Correct 11 ms 6520 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 353 ms 13176 KB Output is correct
2 Correct 349 ms 25848 KB Output is correct
3 Correct 54 ms 9592 KB Output is correct
4 Correct 636 ms 18552 KB Output is correct
5 Correct 745 ms 40952 KB Output is correct
6 Correct 703 ms 29560 KB Output is correct
7 Correct 566 ms 18160 KB Output is correct
8 Correct 709 ms 20576 KB Output is correct
9 Correct 691 ms 19960 KB Output is correct
10 Correct 694 ms 19704 KB Output is correct
11 Correct 432 ms 18696 KB Output is correct
12 Correct 767 ms 32888 KB Output is correct
13 Correct 686 ms 29684 KB Output is correct
14 Correct 684 ms 29428 KB Output is correct
15 Correct 427 ms 18792 KB Output is correct
16 Correct 637 ms 33520 KB Output is correct
17 Correct 656 ms 29684 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 554 ms 16372 KB Output is correct
2 Correct 830 ms 43128 KB Output is correct
3 Correct 73 ms 10232 KB Output is correct
4 Correct 730 ms 19064 KB Output is correct
5 Correct 896 ms 44668 KB Output is correct
6 Correct 838 ms 31480 KB Output is correct
7 Correct 682 ms 18936 KB Output is correct
8 Correct 814 ms 24184 KB Output is correct
9 Correct 767 ms 22392 KB Output is correct
10 Correct 766 ms 20984 KB Output is correct
11 Correct 575 ms 19060 KB Output is correct
12 Correct 899 ms 39420 KB Output is correct
13 Correct 790 ms 29940 KB Output is correct
14 Correct 860 ms 30196 KB Output is correct
15 Correct 430 ms 18672 KB Output is correct
16 Correct 786 ms 36204 KB Output is correct
17 Correct 774 ms 31732 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 6264 KB Output is correct
2 Correct 11 ms 6392 KB Output is correct
3 Correct 10 ms 6520 KB Output is correct
4 Correct 11 ms 6520 KB Output is correct
5 Correct 12 ms 6424 KB Output is correct
6 Correct 11 ms 6652 KB Output is correct
7 Correct 11 ms 6520 KB Output is correct
8 Correct 12 ms 6392 KB Output is correct
9 Correct 13 ms 6392 KB Output is correct
10 Correct 11 ms 6392 KB Output is correct
11 Correct 12 ms 6392 KB Output is correct
12 Correct 11 ms 6392 KB Output is correct
13 Correct 11 ms 6648 KB Output is correct
14 Correct 11 ms 6520 KB Output is correct
15 Correct 11 ms 6520 KB Output is correct
16 Correct 11 ms 6392 KB Output is correct
17 Correct 11 ms 6520 KB Output is correct
18 Correct 11 ms 6520 KB Output is correct
19 Correct 12 ms 6392 KB Output is correct
20 Correct 11 ms 6648 KB Output is correct
21 Correct 12 ms 6520 KB Output is correct
22 Correct 11 ms 6392 KB Output is correct
23 Correct 11 ms 6392 KB Output is correct
24 Correct 12 ms 6396 KB Output is correct
25 Correct 11 ms 6392 KB Output is correct
26 Correct 10 ms 6392 KB Output is correct
27 Correct 12 ms 6520 KB Output is correct
28 Correct 11 ms 6520 KB Output is correct
29 Correct 12 ms 6520 KB Output is correct
30 Correct 10 ms 6392 KB Output is correct
31 Correct 13 ms 6520 KB Output is correct
32 Correct 11 ms 6520 KB Output is correct
33 Correct 353 ms 13176 KB Output is correct
34 Correct 349 ms 25848 KB Output is correct
35 Correct 54 ms 9592 KB Output is correct
36 Correct 636 ms 18552 KB Output is correct
37 Correct 745 ms 40952 KB Output is correct
38 Correct 703 ms 29560 KB Output is correct
39 Correct 566 ms 18160 KB Output is correct
40 Correct 709 ms 20576 KB Output is correct
41 Correct 691 ms 19960 KB Output is correct
42 Correct 694 ms 19704 KB Output is correct
43 Correct 432 ms 18696 KB Output is correct
44 Correct 767 ms 32888 KB Output is correct
45 Correct 686 ms 29684 KB Output is correct
46 Correct 684 ms 29428 KB Output is correct
47 Correct 427 ms 18792 KB Output is correct
48 Correct 637 ms 33520 KB Output is correct
49 Correct 656 ms 29684 KB Output is correct
50 Correct 554 ms 16372 KB Output is correct
51 Correct 830 ms 43128 KB Output is correct
52 Correct 73 ms 10232 KB Output is correct
53 Correct 730 ms 19064 KB Output is correct
54 Correct 896 ms 44668 KB Output is correct
55 Correct 838 ms 31480 KB Output is correct
56 Correct 682 ms 18936 KB Output is correct
57 Correct 814 ms 24184 KB Output is correct
58 Correct 767 ms 22392 KB Output is correct
59 Correct 766 ms 20984 KB Output is correct
60 Correct 575 ms 19060 KB Output is correct
61 Correct 899 ms 39420 KB Output is correct
62 Correct 790 ms 29940 KB Output is correct
63 Correct 860 ms 30196 KB Output is correct
64 Correct 430 ms 18672 KB Output is correct
65 Correct 786 ms 36204 KB Output is correct
66 Correct 774 ms 31732 KB Output is correct
67 Correct 63 ms 8060 KB Output is correct
68 Correct 303 ms 23064 KB Output is correct
69 Correct 427 ms 21112 KB Output is correct
70 Correct 711 ms 18168 KB Output is correct
71 Correct 750 ms 40700 KB Output is correct
72 Correct 730 ms 32504 KB Output is correct
73 Correct 660 ms 21112 KB Output is correct
74 Correct 738 ms 25592 KB Output is correct
75 Correct 711 ms 23160 KB Output is correct
76 Correct 709 ms 22904 KB Output is correct
77 Correct 508 ms 21232 KB Output is correct
78 Correct 810 ms 38392 KB Output is correct
79 Correct 695 ms 35444 KB Output is correct
80 Correct 718 ms 30900 KB Output is correct
81 Correct 451 ms 21484 KB Output is correct
82 Correct 648 ms 36464 KB Output is correct
83 Correct 669 ms 32500 KB Output is correct
84 Correct 716 ms 21240 KB Output is correct
85 Correct 770 ms 44460 KB Output is correct
86 Correct 738 ms 33016 KB Output is correct
87 Correct 655 ms 21496 KB Output is correct
88 Correct 748 ms 26364 KB Output is correct
89 Correct 723 ms 24056 KB Output is correct
90 Correct 753 ms 23480 KB Output is correct
91 Correct 553 ms 21744 KB Output is correct
92 Correct 783 ms 44664 KB Output is correct
93 Correct 761 ms 29812 KB Output is correct
94 Correct 749 ms 27768 KB Output is correct
95 Correct 430 ms 21996 KB Output is correct
96 Correct 662 ms 37100 KB Output is correct
97 Correct 694 ms 33124 KB Output is correct
98 Correct 735 ms 22776 KB Output is correct
99 Correct 788 ms 45176 KB Output is correct
100 Correct 840 ms 35064 KB Output is correct
101 Correct 650 ms 22004 KB Output is correct
102 Correct 807 ms 25976 KB Output is correct
103 Correct 782 ms 24312 KB Output is correct
104 Correct 787 ms 23928 KB Output is correct
105 Correct 526 ms 22124 KB Output is correct
106 Correct 911 ms 36344 KB Output is correct
107 Correct 733 ms 36828 KB Output is correct
108 Correct 827 ms 30328 KB Output is correct
109 Correct 424 ms 22640 KB Output is correct
110 Correct 702 ms 39152 KB Output is correct
111 Correct 765 ms 35192 KB Output is correct