답안 #493994

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
493994 2021-12-13T17:19:36 Z fcmalkcin Mergers (JOI19_mergers) C++17
100 / 100
1088 ms 189652 KB
/*#pragma GCC optimize("Ofast")
#pragma GCC optimization("unroll-loops, no-stack-protector")
#pragma GCC target("avx,avx2,fma")*/

#include <bits/stdc++.h>
using namespace std;

#define ll  long long
#define pll pair<ll,ll>
#define ff first
#define ss second
#define pb push_back
#define endl "\n"
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());

const ll maxn=5e5+20;
const ll mod=998244353  ;
const ll base=1e9+100;

/// you will be the best but now you just are trash
/// goal 5/7

ll anc[maxn][20];
ll par1[maxn];
ll a[maxn];
ll par[maxn];
vector<ll> gr[maxn];
vector<ll> adj[maxn];
ll cntnw=0;
ll f[maxn];
ll dep[maxn];
ll find_par(ll u)
{
    if (u==par[u]) return u;
    return par[u]=find_par(par[u]);
}
void dsu(ll x,ll y)
{
    x=find_par(x);
    y=find_par(y);
    if (x==y)
    {
        return ;
    }
    if (dep[x]<dep[y]) swap(x,y);
    par[x]=y;
}
void dfs(ll u,ll par)
{
    cntnw++;
    f[u]=cntnw;
    anc[u][0]=par;
    for (int i=1;i<20;i++) anc[u][i]=anc[anc[u][i-1]][i-1];
    par1[u]=par;
    for (auto to:adj[u])
    {
        if (to==par) continue;
        dep[to]=dep[u]+1;
        dfs(to,u);
    }
}
ll lca(ll x,ll y)
{
    if (dep[x]<dep[y]) swap(x,y);
    ll kc=dep[x]-dep[y];
    for (int i=19;i>=0;i--)
    {
        if (kc&(1ll<<i))
        {
            x=anc[x][i];
        }
    }
    if (x==y) return x;
    for (int i=19;i>=0;i--)
    {
        if (anc[x][i]!=anc[y][i])
        {
            x=anc[x][i];
            y=anc[y][i];
        }
    }
    return anc[x][0];
}
ll cnt[maxn];

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    if (fopen("test.inp", "r"))
    {
        freopen("test.inp", "r", stdin);
        freopen("test.out", "w", stdout);
    }
    ll n, k;
    cin>> n>> k;
    vector<pll> vt;
    for (int i=1;i<=n-1;i++)
    {
        ll x, y;
        cin>>x>> y;
        vt.pb(make_pair(x,y));
        adj[x].pb(y);
        adj[y].pb(x);
    }
    for (int i=1;i<=n;i++)
    {
        par[i]=i;
        cin>>a[i];
        gr[a[i]].pb(i);
    }
    dfs(1,0);
    for (int i=1;i<=n;i++)
    {
        for (auto to:adj[i])
        {
            if (a[to]==a[i]) dsu(to,i);
        }
    }
    for (int i=1;i<=k;i++)
    {
        set<pll> st;
        for (auto p:gr[i])
        {
            ll x=find_par(p);
            st.insert(make_pair(f[x],x));
         //   if (i==1) cout <<f[x]<<" "<<x<<" "<<p<<endl;
        }
        gr[i].clear();
        while (st.size()>=2)
        {
            auto p=(*st.begin());
            st.erase(st.begin());
            auto p1=(*st.begin());
            st.erase(st.begin());
            ll pos=p.ss;
            ll pos1=p1.ss;
            vector<ll> vt;
            ll nw=lca(pos,pos1);
            nw=find_par(nw);

            while (dep[nw]<dep[find_par(pos)])
            {
                pos=find_par(pos);
                ll h=par1[pos];
                dsu(pos,h);

                for (auto to:gr[a[h]])
                {
                    vt.pb(to);
                }
                gr[a[h]].clear();
            }
            swap(pos,pos1);
          //  cout <<pos<<endl;
             while (dep[nw]<dep[find_par(pos)])
            {
                pos=find_par(pos);
                ll h=par1[pos];
                dsu(pos,h);
               // cout <<pos<<" "<<h<<endl;
                for (auto to:gr[a[h]])
                {
                    vt.pb(to);
                }
                gr[a[h]].clear();
            }
            for (auto to:vt)
            {
                ll x=find_par(to);
                st.insert(make_pair(f[x],x));
            }
            ll h=find_par(pos);
            st.insert(make_pair(f[h],h));
        }
    }
    /*for (int i=1;i<=n;i++)
    {
        cout <<i<<" "<<find_par(i)<<endl;
    }*/
    for (auto to:vt)
    {
        ll x=find_par(to.ff);
        ll y=find_par(to.ss);
        if (x!=y)
        {
           cnt[x]++;
           cnt[y]++;
        }
    }
    ll ans=0;
    ll dem=0;
    for (int i=1;i<=n;i++)
    {
        if (i==find_par(i))
        {
         //  cout <<i<<" "<<cnt[i]<<endl;
            dem++;
            if (cnt[i]==1) ans++;
        }
    }
    if (dem==1) cout <<0<<endl;
    else cout <<(ans+1)/2<<endl;


}

Compilation message

mergers.cpp: In function 'int main()':
mergers.cpp:93:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   93 |         freopen("test.inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
mergers.cpp:94:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   94 |         freopen("test.out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 23864 KB Output is correct
2 Correct 11 ms 23884 KB Output is correct
3 Correct 14 ms 23840 KB Output is correct
4 Correct 16 ms 23796 KB Output is correct
5 Correct 12 ms 23824 KB Output is correct
6 Correct 11 ms 23772 KB Output is correct
7 Correct 11 ms 23780 KB Output is correct
8 Correct 12 ms 23884 KB Output is correct
9 Correct 11 ms 23756 KB Output is correct
10 Correct 11 ms 23756 KB Output is correct
11 Correct 11 ms 23892 KB Output is correct
12 Correct 12 ms 23764 KB Output is correct
13 Correct 12 ms 23792 KB Output is correct
14 Correct 12 ms 23848 KB Output is correct
15 Correct 11 ms 23884 KB Output is correct
16 Correct 13 ms 23756 KB Output is correct
17 Correct 12 ms 23816 KB Output is correct
18 Correct 11 ms 23884 KB Output is correct
19 Correct 12 ms 23756 KB Output is correct
20 Correct 12 ms 23816 KB Output is correct
21 Correct 12 ms 23888 KB Output is correct
22 Correct 13 ms 23884 KB Output is correct
23 Correct 15 ms 23856 KB Output is correct
24 Correct 11 ms 23756 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 23864 KB Output is correct
2 Correct 11 ms 23884 KB Output is correct
3 Correct 14 ms 23840 KB Output is correct
4 Correct 16 ms 23796 KB Output is correct
5 Correct 12 ms 23824 KB Output is correct
6 Correct 11 ms 23772 KB Output is correct
7 Correct 11 ms 23780 KB Output is correct
8 Correct 12 ms 23884 KB Output is correct
9 Correct 11 ms 23756 KB Output is correct
10 Correct 11 ms 23756 KB Output is correct
11 Correct 11 ms 23892 KB Output is correct
12 Correct 12 ms 23764 KB Output is correct
13 Correct 12 ms 23792 KB Output is correct
14 Correct 12 ms 23848 KB Output is correct
15 Correct 11 ms 23884 KB Output is correct
16 Correct 13 ms 23756 KB Output is correct
17 Correct 12 ms 23816 KB Output is correct
18 Correct 11 ms 23884 KB Output is correct
19 Correct 12 ms 23756 KB Output is correct
20 Correct 12 ms 23816 KB Output is correct
21 Correct 12 ms 23888 KB Output is correct
22 Correct 13 ms 23884 KB Output is correct
23 Correct 15 ms 23856 KB Output is correct
24 Correct 11 ms 23756 KB Output is correct
25 Correct 11 ms 23820 KB Output is correct
26 Correct 14 ms 24708 KB Output is correct
27 Correct 13 ms 24652 KB Output is correct
28 Correct 15 ms 24780 KB Output is correct
29 Correct 16 ms 24780 KB Output is correct
30 Correct 16 ms 24652 KB Output is correct
31 Correct 15 ms 23764 KB Output is correct
32 Correct 16 ms 24780 KB Output is correct
33 Correct 13 ms 23884 KB Output is correct
34 Correct 15 ms 24684 KB Output is correct
35 Correct 15 ms 24780 KB Output is correct
36 Correct 13 ms 24724 KB Output is correct
37 Correct 14 ms 24652 KB Output is correct
38 Correct 13 ms 23884 KB Output is correct
39 Correct 14 ms 24732 KB Output is correct
40 Correct 14 ms 24616 KB Output is correct
41 Correct 14 ms 24652 KB Output is correct
42 Correct 15 ms 24652 KB Output is correct
43 Correct 16 ms 24752 KB Output is correct
44 Correct 13 ms 23820 KB Output is correct
45 Correct 15 ms 24728 KB Output is correct
46 Correct 14 ms 24652 KB Output is correct
47 Correct 13 ms 23808 KB Output is correct
48 Correct 14 ms 24720 KB Output is correct
49 Correct 15 ms 24780 KB Output is correct
50 Correct 15 ms 24780 KB Output is correct
51 Correct 14 ms 24624 KB Output is correct
52 Correct 14 ms 24652 KB Output is correct
53 Correct 14 ms 24692 KB Output is correct
54 Correct 19 ms 24792 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 23864 KB Output is correct
2 Correct 11 ms 23884 KB Output is correct
3 Correct 14 ms 23840 KB Output is correct
4 Correct 16 ms 23796 KB Output is correct
5 Correct 12 ms 23824 KB Output is correct
6 Correct 11 ms 23772 KB Output is correct
7 Correct 11 ms 23780 KB Output is correct
8 Correct 12 ms 23884 KB Output is correct
9 Correct 11 ms 23756 KB Output is correct
10 Correct 11 ms 23756 KB Output is correct
11 Correct 11 ms 23892 KB Output is correct
12 Correct 12 ms 23764 KB Output is correct
13 Correct 12 ms 23792 KB Output is correct
14 Correct 12 ms 23848 KB Output is correct
15 Correct 11 ms 23884 KB Output is correct
16 Correct 13 ms 23756 KB Output is correct
17 Correct 12 ms 23816 KB Output is correct
18 Correct 11 ms 23884 KB Output is correct
19 Correct 12 ms 23756 KB Output is correct
20 Correct 12 ms 23816 KB Output is correct
21 Correct 12 ms 23888 KB Output is correct
22 Correct 13 ms 23884 KB Output is correct
23 Correct 15 ms 23856 KB Output is correct
24 Correct 11 ms 23756 KB Output is correct
25 Correct 13 ms 23884 KB Output is correct
26 Correct 116 ms 51336 KB Output is correct
27 Correct 175 ms 57128 KB Output is correct
28 Correct 14 ms 24704 KB Output is correct
29 Correct 13 ms 23868 KB Output is correct
30 Correct 12 ms 23836 KB Output is correct
31 Correct 111 ms 51264 KB Output is correct
32 Correct 15 ms 24720 KB Output is correct
33 Correct 222 ms 60828 KB Output is correct
34 Correct 167 ms 54436 KB Output is correct
35 Correct 14 ms 24652 KB Output is correct
36 Correct 127 ms 51172 KB Output is correct
37 Correct 14 ms 24652 KB Output is correct
38 Correct 16 ms 24644 KB Output is correct
39 Correct 127 ms 51456 KB Output is correct
40 Correct 15 ms 24752 KB Output is correct
41 Correct 118 ms 51876 KB Output is correct
42 Correct 181 ms 55728 KB Output is correct
43 Correct 13 ms 23872 KB Output is correct
44 Correct 128 ms 55944 KB Output is correct
45 Correct 192 ms 59972 KB Output is correct
46 Correct 16 ms 24716 KB Output is correct
47 Correct 15 ms 24856 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 116 ms 49988 KB Output is correct
2 Correct 105 ms 52768 KB Output is correct
3 Correct 14 ms 24652 KB Output is correct
4 Correct 14 ms 24600 KB Output is correct
5 Correct 12 ms 23884 KB Output is correct
6 Correct 12 ms 23844 KB Output is correct
7 Correct 14 ms 24604 KB Output is correct
8 Correct 140 ms 53000 KB Output is correct
9 Correct 14 ms 24652 KB Output is correct
10 Correct 109 ms 52192 KB Output is correct
11 Correct 13 ms 23820 KB Output is correct
12 Correct 127 ms 51564 KB Output is correct
13 Correct 145 ms 52900 KB Output is correct
14 Correct 127 ms 54384 KB Output is correct
15 Correct 125 ms 51312 KB Output is correct
16 Correct 14 ms 24764 KB Output is correct
17 Correct 12 ms 23884 KB Output is correct
18 Correct 107 ms 54192 KB Output is correct
19 Correct 148 ms 56180 KB Output is correct
20 Correct 14 ms 24652 KB Output is correct
21 Correct 12 ms 23860 KB Output is correct
22 Correct 117 ms 53124 KB Output is correct
23 Correct 14 ms 24652 KB Output is correct
24 Correct 124 ms 52352 KB Output is correct
25 Correct 144 ms 56236 KB Output is correct
26 Correct 15 ms 24764 KB Output is correct
27 Correct 15 ms 24780 KB Output is correct
28 Correct 15 ms 24652 KB Output is correct
29 Correct 14 ms 24652 KB Output is correct
30 Correct 15 ms 24652 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 23864 KB Output is correct
2 Correct 11 ms 23884 KB Output is correct
3 Correct 14 ms 23840 KB Output is correct
4 Correct 16 ms 23796 KB Output is correct
5 Correct 12 ms 23824 KB Output is correct
6 Correct 11 ms 23772 KB Output is correct
7 Correct 11 ms 23780 KB Output is correct
8 Correct 12 ms 23884 KB Output is correct
9 Correct 11 ms 23756 KB Output is correct
10 Correct 11 ms 23756 KB Output is correct
11 Correct 11 ms 23892 KB Output is correct
12 Correct 12 ms 23764 KB Output is correct
13 Correct 12 ms 23792 KB Output is correct
14 Correct 12 ms 23848 KB Output is correct
15 Correct 11 ms 23884 KB Output is correct
16 Correct 13 ms 23756 KB Output is correct
17 Correct 12 ms 23816 KB Output is correct
18 Correct 11 ms 23884 KB Output is correct
19 Correct 12 ms 23756 KB Output is correct
20 Correct 12 ms 23816 KB Output is correct
21 Correct 12 ms 23888 KB Output is correct
22 Correct 13 ms 23884 KB Output is correct
23 Correct 15 ms 23856 KB Output is correct
24 Correct 11 ms 23756 KB Output is correct
25 Correct 11 ms 23820 KB Output is correct
26 Correct 14 ms 24708 KB Output is correct
27 Correct 13 ms 24652 KB Output is correct
28 Correct 15 ms 24780 KB Output is correct
29 Correct 16 ms 24780 KB Output is correct
30 Correct 16 ms 24652 KB Output is correct
31 Correct 15 ms 23764 KB Output is correct
32 Correct 16 ms 24780 KB Output is correct
33 Correct 13 ms 23884 KB Output is correct
34 Correct 15 ms 24684 KB Output is correct
35 Correct 15 ms 24780 KB Output is correct
36 Correct 13 ms 24724 KB Output is correct
37 Correct 14 ms 24652 KB Output is correct
38 Correct 13 ms 23884 KB Output is correct
39 Correct 14 ms 24732 KB Output is correct
40 Correct 14 ms 24616 KB Output is correct
41 Correct 14 ms 24652 KB Output is correct
42 Correct 15 ms 24652 KB Output is correct
43 Correct 16 ms 24752 KB Output is correct
44 Correct 13 ms 23820 KB Output is correct
45 Correct 15 ms 24728 KB Output is correct
46 Correct 14 ms 24652 KB Output is correct
47 Correct 13 ms 23808 KB Output is correct
48 Correct 14 ms 24720 KB Output is correct
49 Correct 15 ms 24780 KB Output is correct
50 Correct 15 ms 24780 KB Output is correct
51 Correct 14 ms 24624 KB Output is correct
52 Correct 14 ms 24652 KB Output is correct
53 Correct 14 ms 24692 KB Output is correct
54 Correct 19 ms 24792 KB Output is correct
55 Correct 13 ms 23884 KB Output is correct
56 Correct 116 ms 51336 KB Output is correct
57 Correct 175 ms 57128 KB Output is correct
58 Correct 14 ms 24704 KB Output is correct
59 Correct 13 ms 23868 KB Output is correct
60 Correct 12 ms 23836 KB Output is correct
61 Correct 111 ms 51264 KB Output is correct
62 Correct 15 ms 24720 KB Output is correct
63 Correct 222 ms 60828 KB Output is correct
64 Correct 167 ms 54436 KB Output is correct
65 Correct 14 ms 24652 KB Output is correct
66 Correct 127 ms 51172 KB Output is correct
67 Correct 14 ms 24652 KB Output is correct
68 Correct 16 ms 24644 KB Output is correct
69 Correct 127 ms 51456 KB Output is correct
70 Correct 15 ms 24752 KB Output is correct
71 Correct 118 ms 51876 KB Output is correct
72 Correct 181 ms 55728 KB Output is correct
73 Correct 13 ms 23872 KB Output is correct
74 Correct 128 ms 55944 KB Output is correct
75 Correct 192 ms 59972 KB Output is correct
76 Correct 16 ms 24716 KB Output is correct
77 Correct 15 ms 24856 KB Output is correct
78 Correct 116 ms 49988 KB Output is correct
79 Correct 105 ms 52768 KB Output is correct
80 Correct 14 ms 24652 KB Output is correct
81 Correct 14 ms 24600 KB Output is correct
82 Correct 12 ms 23884 KB Output is correct
83 Correct 12 ms 23844 KB Output is correct
84 Correct 14 ms 24604 KB Output is correct
85 Correct 140 ms 53000 KB Output is correct
86 Correct 14 ms 24652 KB Output is correct
87 Correct 109 ms 52192 KB Output is correct
88 Correct 13 ms 23820 KB Output is correct
89 Correct 127 ms 51564 KB Output is correct
90 Correct 145 ms 52900 KB Output is correct
91 Correct 127 ms 54384 KB Output is correct
92 Correct 125 ms 51312 KB Output is correct
93 Correct 14 ms 24764 KB Output is correct
94 Correct 12 ms 23884 KB Output is correct
95 Correct 107 ms 54192 KB Output is correct
96 Correct 148 ms 56180 KB Output is correct
97 Correct 14 ms 24652 KB Output is correct
98 Correct 12 ms 23860 KB Output is correct
99 Correct 117 ms 53124 KB Output is correct
100 Correct 14 ms 24652 KB Output is correct
101 Correct 124 ms 52352 KB Output is correct
102 Correct 144 ms 56236 KB Output is correct
103 Correct 15 ms 24764 KB Output is correct
104 Correct 15 ms 24780 KB Output is correct
105 Correct 15 ms 24652 KB Output is correct
106 Correct 14 ms 24652 KB Output is correct
107 Correct 15 ms 24652 KB Output is correct
108 Correct 758 ms 171500 KB Output is correct
109 Correct 897 ms 171224 KB Output is correct
110 Correct 885 ms 178612 KB Output is correct
111 Correct 1069 ms 189652 KB Output is correct
112 Correct 1055 ms 188940 KB Output is correct
113 Correct 739 ms 177088 KB Output is correct
114 Correct 730 ms 161216 KB Output is correct
115 Correct 726 ms 163868 KB Output is correct
116 Correct 986 ms 167832 KB Output is correct
117 Correct 865 ms 177760 KB Output is correct
118 Correct 743 ms 162256 KB Output is correct
119 Correct 938 ms 177664 KB Output is correct
120 Correct 1088 ms 187592 KB Output is correct
121 Correct 890 ms 177660 KB Output is correct
122 Correct 1018 ms 173656 KB Output is correct
123 Correct 739 ms 178612 KB Output is correct
124 Correct 1002 ms 171872 KB Output is correct