답안 #1045393

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1045393 2024-08-05T22:50:38 Z RauPro Mergers (JOI19_mergers) C++14
100 / 100
319 ms 87124 KB
/*
    -For every state, we know that every vertex that is on the shortest path between any two vertexes belonging to the state must be in the same final group.
    -Do this merging for every state and compress the tree based on which vertexes must belong to the same final group.
    -Our goal is to compress this tree(the compressed tree) into one vertex by merging some states together.
    -This is equivalent to the problem of: Choose minimum number of pairs of vertexes in the tree such that if we mark all the vertexes on the simple path between the vertexes of each pair, we will mark every vertex.
    -This can be done by sorting all the leaves(its optimal to choose only leaves) of the tree in order in which they appear in the euler tour of the tree.
    -Choose vertex number 1 and n/2+1,2 and n/2+2 and so on. There are (n+1)/2 of these pairs. n is the number of leaves.
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>

#define ll long long
#define pb push_back
#define sz(x) (int)(x).size()
#define mp make_pair
#define f first
#define s second
#define all(x) x.begin(), x.end()
#define D(x) cerr << #x << " is " << (x) << "\n";

using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>; ///find_by_order(),order_of_key()
template<class T1, class T2> ostream& operator<<(ostream& os, const pair<T1,T2>& a) { os << '{' << a.f << ", " << a.s << '}'; return os; }
template<class T> ostream& operator<<(ostream& os, const vector<T>& a){os << '{';for(int i=0;i<sz(a);i++){if(i>0&&i<sz(a))os << ", ";os << a[i];}os<<'}';return os;}
template<class T> ostream& operator<<(ostream& os, const set<T>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T> ostream& operator<<(ostream& os, const multiset<T>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T1,class T2> ostream& operator<<(ostream& os, const map<T1,T2>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}

const int N=5e5+5;
struct dsu{
    vector<int> par;
    void init()
    {
        par.resize(N);
        for(int i=0;i<N;i++)
            par[i]=i;
    }
    int find(int tr)
    {
        if(par[tr]==tr)
            return tr;
        return par[tr]=find(par[tr]);
    }
    void merge(int a,int b)
    {
        a=find(a);
        b=find(b);
        if(a==b)
            return;
        par[b]=a;
    }
}d;
vector<int> up(N),depth(N);
vector<vector<int> > graf(N);
void setup(int tr)
{
    for(auto p:graf[tr])
    {
        if(p==up[tr])
            continue;
        depth[p]=depth[tr]+1;
        up[p]=tr;
        setup(p);
    }
}
void merge(int a,int b)
{
    a=d.find(a);b=d.find(b);
    while(a!=b)
    {
        if(depth[a]<depth[b])
            swap(a,b);
        d.merge(up[a],a);
        a=d.find(up[a]);
    }
}
int main()
{
    d.init();
    int n,k;
    scanf("%i %i",&n,&k);
    for(int i=1;i<n;i++)
    {
        int a,b;
        scanf("%i %i",&a,&b);
        a--;
        b--;
        graf[a].pb(b);
        graf[b].pb(a);
    }
    setup(0);
    vector<vector<int> > group(k);
    for(int i=0;i<n;i++)
    {
        int a;
        scanf("%i",&a);
        a--;
        group[a].pb(i);
    }
    for(int i=0;i<k;i++)
        for(int j=1;j<group[i].size();j++)
            merge(group[i][0],group[i][j]);
    int sol=0;
    vector<int> deg(n);
    for(int i=0;i<n;i++)
        for(auto p:graf[i])
            if(d.find(i)!=d.find(p))
            {
                if(deg[d.find(i)]==0)
                    sol++;
                if(deg[d.find(i)]==1)
                    sol--;
                deg[d.find(i)]++;
            }
    printf("%i\n",(sol+1)/2);
    return 0;
}

Compilation message

mergers.cpp: In function 'int main()':
mergers.cpp:107:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  107 |         for(int j=1;j<group[i].size();j++)
      |                     ~^~~~~~~~~~~~~~~~
mergers.cpp:87:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   87 |     scanf("%i %i",&n,&k);
      |     ~~~~~^~~~~~~~~~~~~~~
mergers.cpp:91:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   91 |         scanf("%i %i",&a,&b);
      |         ~~~~~^~~~~~~~~~~~~~~
mergers.cpp:102:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  102 |         scanf("%i",&a);
      |         ~~~~~^~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 18012 KB Output is correct
2 Correct 4 ms 18012 KB Output is correct
3 Correct 4 ms 18012 KB Output is correct
4 Correct 4 ms 18012 KB Output is correct
5 Correct 5 ms 18008 KB Output is correct
6 Correct 5 ms 18012 KB Output is correct
7 Correct 4 ms 18012 KB Output is correct
8 Correct 5 ms 18012 KB Output is correct
9 Correct 4 ms 18012 KB Output is correct
10 Correct 5 ms 17936 KB Output is correct
11 Correct 5 ms 18008 KB Output is correct
12 Correct 4 ms 18012 KB Output is correct
13 Correct 5 ms 18072 KB Output is correct
14 Correct 4 ms 18008 KB Output is correct
15 Correct 4 ms 17988 KB Output is correct
16 Correct 4 ms 18008 KB Output is correct
17 Correct 5 ms 18264 KB Output is correct
18 Correct 5 ms 18012 KB Output is correct
19 Correct 4 ms 18012 KB Output is correct
20 Correct 4 ms 18012 KB Output is correct
21 Correct 4 ms 18012 KB Output is correct
22 Correct 5 ms 18012 KB Output is correct
23 Correct 4 ms 18012 KB Output is correct
24 Correct 5 ms 18012 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 18012 KB Output is correct
2 Correct 4 ms 18012 KB Output is correct
3 Correct 4 ms 18012 KB Output is correct
4 Correct 4 ms 18012 KB Output is correct
5 Correct 5 ms 18008 KB Output is correct
6 Correct 5 ms 18012 KB Output is correct
7 Correct 4 ms 18012 KB Output is correct
8 Correct 5 ms 18012 KB Output is correct
9 Correct 4 ms 18012 KB Output is correct
10 Correct 5 ms 17936 KB Output is correct
11 Correct 5 ms 18008 KB Output is correct
12 Correct 4 ms 18012 KB Output is correct
13 Correct 5 ms 18072 KB Output is correct
14 Correct 4 ms 18008 KB Output is correct
15 Correct 4 ms 17988 KB Output is correct
16 Correct 4 ms 18008 KB Output is correct
17 Correct 5 ms 18264 KB Output is correct
18 Correct 5 ms 18012 KB Output is correct
19 Correct 4 ms 18012 KB Output is correct
20 Correct 4 ms 18012 KB Output is correct
21 Correct 4 ms 18012 KB Output is correct
22 Correct 5 ms 18012 KB Output is correct
23 Correct 4 ms 18012 KB Output is correct
24 Correct 5 ms 18012 KB Output is correct
25 Correct 4 ms 18012 KB Output is correct
26 Correct 5 ms 18124 KB Output is correct
27 Correct 5 ms 18012 KB Output is correct
28 Correct 5 ms 18208 KB Output is correct
29 Correct 7 ms 18268 KB Output is correct
30 Correct 6 ms 18012 KB Output is correct
31 Correct 4 ms 18012 KB Output is correct
32 Correct 5 ms 18268 KB Output is correct
33 Correct 5 ms 18012 KB Output is correct
34 Correct 5 ms 18012 KB Output is correct
35 Correct 6 ms 18268 KB Output is correct
36 Correct 5 ms 18012 KB Output is correct
37 Correct 5 ms 18268 KB Output is correct
38 Correct 4 ms 18012 KB Output is correct
39 Correct 5 ms 18268 KB Output is correct
40 Correct 6 ms 18008 KB Output is correct
41 Correct 5 ms 18016 KB Output is correct
42 Correct 5 ms 18268 KB Output is correct
43 Correct 6 ms 18268 KB Output is correct
44 Correct 5 ms 18008 KB Output is correct
45 Correct 5 ms 18264 KB Output is correct
46 Correct 5 ms 18184 KB Output is correct
47 Correct 4 ms 18080 KB Output is correct
48 Correct 6 ms 18012 KB Output is correct
49 Correct 6 ms 18268 KB Output is correct
50 Correct 5 ms 18268 KB Output is correct
51 Correct 5 ms 18268 KB Output is correct
52 Correct 5 ms 18012 KB Output is correct
53 Correct 5 ms 18268 KB Output is correct
54 Correct 6 ms 18008 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 18012 KB Output is correct
2 Correct 4 ms 18012 KB Output is correct
3 Correct 4 ms 18012 KB Output is correct
4 Correct 4 ms 18012 KB Output is correct
5 Correct 5 ms 18008 KB Output is correct
6 Correct 5 ms 18012 KB Output is correct
7 Correct 4 ms 18012 KB Output is correct
8 Correct 5 ms 18012 KB Output is correct
9 Correct 4 ms 18012 KB Output is correct
10 Correct 5 ms 17936 KB Output is correct
11 Correct 5 ms 18008 KB Output is correct
12 Correct 4 ms 18012 KB Output is correct
13 Correct 5 ms 18072 KB Output is correct
14 Correct 4 ms 18008 KB Output is correct
15 Correct 4 ms 17988 KB Output is correct
16 Correct 4 ms 18008 KB Output is correct
17 Correct 5 ms 18264 KB Output is correct
18 Correct 5 ms 18012 KB Output is correct
19 Correct 4 ms 18012 KB Output is correct
20 Correct 4 ms 18012 KB Output is correct
21 Correct 4 ms 18012 KB Output is correct
22 Correct 5 ms 18012 KB Output is correct
23 Correct 4 ms 18012 KB Output is correct
24 Correct 5 ms 18012 KB Output is correct
25 Correct 5 ms 18012 KB Output is correct
26 Correct 39 ms 23736 KB Output is correct
27 Correct 36 ms 23380 KB Output is correct
28 Correct 6 ms 18012 KB Output is correct
29 Correct 4 ms 18012 KB Output is correct
30 Correct 4 ms 18008 KB Output is correct
31 Correct 35 ms 23480 KB Output is correct
32 Correct 5 ms 18012 KB Output is correct
33 Correct 40 ms 27728 KB Output is correct
34 Correct 36 ms 23620 KB Output is correct
35 Correct 5 ms 18012 KB Output is correct
36 Correct 45 ms 23888 KB Output is correct
37 Correct 6 ms 18008 KB Output is correct
38 Correct 4 ms 18012 KB Output is correct
39 Correct 32 ms 23640 KB Output is correct
40 Correct 5 ms 18264 KB Output is correct
41 Correct 38 ms 23508 KB Output is correct
42 Correct 41 ms 25200 KB Output is correct
43 Correct 4 ms 18008 KB Output is correct
44 Correct 36 ms 27784 KB Output is correct
45 Correct 36 ms 25680 KB Output is correct
46 Correct 5 ms 18012 KB Output is correct
47 Correct 5 ms 18012 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 37 ms 23920 KB Output is correct
2 Correct 38 ms 29132 KB Output is correct
3 Correct 6 ms 18268 KB Output is correct
4 Correct 5 ms 18012 KB Output is correct
5 Correct 4 ms 18012 KB Output is correct
6 Correct 5 ms 17888 KB Output is correct
7 Correct 5 ms 18012 KB Output is correct
8 Correct 44 ms 25988 KB Output is correct
9 Correct 6 ms 18012 KB Output is correct
10 Correct 36 ms 23700 KB Output is correct
11 Correct 5 ms 18012 KB Output is correct
12 Correct 36 ms 23592 KB Output is correct
13 Correct 56 ms 25956 KB Output is correct
14 Correct 46 ms 28892 KB Output is correct
15 Correct 37 ms 23884 KB Output is correct
16 Correct 6 ms 18268 KB Output is correct
17 Correct 4 ms 18012 KB Output is correct
18 Correct 36 ms 28504 KB Output is correct
19 Correct 39 ms 31060 KB Output is correct
20 Correct 6 ms 18264 KB Output is correct
21 Correct 4 ms 18012 KB Output is correct
22 Correct 37 ms 26208 KB Output is correct
23 Correct 6 ms 18264 KB Output is correct
24 Correct 40 ms 24356 KB Output is correct
25 Correct 38 ms 30296 KB Output is correct
26 Correct 6 ms 18184 KB Output is correct
27 Correct 5 ms 18268 KB Output is correct
28 Correct 5 ms 18268 KB Output is correct
29 Correct 4 ms 18012 KB Output is correct
30 Correct 5 ms 18268 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 18012 KB Output is correct
2 Correct 4 ms 18012 KB Output is correct
3 Correct 4 ms 18012 KB Output is correct
4 Correct 4 ms 18012 KB Output is correct
5 Correct 5 ms 18008 KB Output is correct
6 Correct 5 ms 18012 KB Output is correct
7 Correct 4 ms 18012 KB Output is correct
8 Correct 5 ms 18012 KB Output is correct
9 Correct 4 ms 18012 KB Output is correct
10 Correct 5 ms 17936 KB Output is correct
11 Correct 5 ms 18008 KB Output is correct
12 Correct 4 ms 18012 KB Output is correct
13 Correct 5 ms 18072 KB Output is correct
14 Correct 4 ms 18008 KB Output is correct
15 Correct 4 ms 17988 KB Output is correct
16 Correct 4 ms 18008 KB Output is correct
17 Correct 5 ms 18264 KB Output is correct
18 Correct 5 ms 18012 KB Output is correct
19 Correct 4 ms 18012 KB Output is correct
20 Correct 4 ms 18012 KB Output is correct
21 Correct 4 ms 18012 KB Output is correct
22 Correct 5 ms 18012 KB Output is correct
23 Correct 4 ms 18012 KB Output is correct
24 Correct 5 ms 18012 KB Output is correct
25 Correct 4 ms 18012 KB Output is correct
26 Correct 5 ms 18124 KB Output is correct
27 Correct 5 ms 18012 KB Output is correct
28 Correct 5 ms 18208 KB Output is correct
29 Correct 7 ms 18268 KB Output is correct
30 Correct 6 ms 18012 KB Output is correct
31 Correct 4 ms 18012 KB Output is correct
32 Correct 5 ms 18268 KB Output is correct
33 Correct 5 ms 18012 KB Output is correct
34 Correct 5 ms 18012 KB Output is correct
35 Correct 6 ms 18268 KB Output is correct
36 Correct 5 ms 18012 KB Output is correct
37 Correct 5 ms 18268 KB Output is correct
38 Correct 4 ms 18012 KB Output is correct
39 Correct 5 ms 18268 KB Output is correct
40 Correct 6 ms 18008 KB Output is correct
41 Correct 5 ms 18016 KB Output is correct
42 Correct 5 ms 18268 KB Output is correct
43 Correct 6 ms 18268 KB Output is correct
44 Correct 5 ms 18008 KB Output is correct
45 Correct 5 ms 18264 KB Output is correct
46 Correct 5 ms 18184 KB Output is correct
47 Correct 4 ms 18080 KB Output is correct
48 Correct 6 ms 18012 KB Output is correct
49 Correct 6 ms 18268 KB Output is correct
50 Correct 5 ms 18268 KB Output is correct
51 Correct 5 ms 18268 KB Output is correct
52 Correct 5 ms 18012 KB Output is correct
53 Correct 5 ms 18268 KB Output is correct
54 Correct 6 ms 18008 KB Output is correct
55 Correct 5 ms 18012 KB Output is correct
56 Correct 39 ms 23736 KB Output is correct
57 Correct 36 ms 23380 KB Output is correct
58 Correct 6 ms 18012 KB Output is correct
59 Correct 4 ms 18012 KB Output is correct
60 Correct 4 ms 18008 KB Output is correct
61 Correct 35 ms 23480 KB Output is correct
62 Correct 5 ms 18012 KB Output is correct
63 Correct 40 ms 27728 KB Output is correct
64 Correct 36 ms 23620 KB Output is correct
65 Correct 5 ms 18012 KB Output is correct
66 Correct 45 ms 23888 KB Output is correct
67 Correct 6 ms 18008 KB Output is correct
68 Correct 4 ms 18012 KB Output is correct
69 Correct 32 ms 23640 KB Output is correct
70 Correct 5 ms 18264 KB Output is correct
71 Correct 38 ms 23508 KB Output is correct
72 Correct 41 ms 25200 KB Output is correct
73 Correct 4 ms 18008 KB Output is correct
74 Correct 36 ms 27784 KB Output is correct
75 Correct 36 ms 25680 KB Output is correct
76 Correct 5 ms 18012 KB Output is correct
77 Correct 5 ms 18012 KB Output is correct
78 Correct 37 ms 23920 KB Output is correct
79 Correct 38 ms 29132 KB Output is correct
80 Correct 6 ms 18268 KB Output is correct
81 Correct 5 ms 18012 KB Output is correct
82 Correct 4 ms 18012 KB Output is correct
83 Correct 5 ms 17888 KB Output is correct
84 Correct 5 ms 18012 KB Output is correct
85 Correct 44 ms 25988 KB Output is correct
86 Correct 6 ms 18012 KB Output is correct
87 Correct 36 ms 23700 KB Output is correct
88 Correct 5 ms 18012 KB Output is correct
89 Correct 36 ms 23592 KB Output is correct
90 Correct 56 ms 25956 KB Output is correct
91 Correct 46 ms 28892 KB Output is correct
92 Correct 37 ms 23884 KB Output is correct
93 Correct 6 ms 18268 KB Output is correct
94 Correct 4 ms 18012 KB Output is correct
95 Correct 36 ms 28504 KB Output is correct
96 Correct 39 ms 31060 KB Output is correct
97 Correct 6 ms 18264 KB Output is correct
98 Correct 4 ms 18012 KB Output is correct
99 Correct 37 ms 26208 KB Output is correct
100 Correct 6 ms 18264 KB Output is correct
101 Correct 40 ms 24356 KB Output is correct
102 Correct 38 ms 30296 KB Output is correct
103 Correct 6 ms 18184 KB Output is correct
104 Correct 5 ms 18268 KB Output is correct
105 Correct 5 ms 18268 KB Output is correct
106 Correct 4 ms 18012 KB Output is correct
107 Correct 5 ms 18268 KB Output is correct
108 Correct 193 ms 58564 KB Output is correct
109 Correct 281 ms 56360 KB Output is correct
110 Correct 317 ms 63028 KB Output is correct
111 Correct 319 ms 87124 KB Output is correct
112 Correct 306 ms 81216 KB Output is correct
113 Correct 224 ms 72228 KB Output is correct
114 Correct 227 ms 45804 KB Output is correct
115 Correct 239 ms 45768 KB Output is correct
116 Correct 244 ms 52920 KB Output is correct
117 Correct 253 ms 73556 KB Output is correct
118 Correct 222 ms 47184 KB Output is correct
119 Correct 253 ms 73552 KB Output is correct
120 Correct 278 ms 81620 KB Output is correct
121 Correct 286 ms 73552 KB Output is correct
122 Correct 268 ms 62544 KB Output is correct
123 Correct 188 ms 74944 KB Output is correct
124 Correct 252 ms 72016 KB Output is correct