답안 #821293

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
821293 2023-08-11T08:46:36 Z vnm06 Marshmallow Molecules (CCO19_day2problem2) C++14
25 / 25
117 ms 33888 KB
#include<bits/stdc++.h>
#define endl '\n'

using namespace std;

int n, q;
pair<int, int> a[100005];
int brr=0;
int loc[100005];
int par[100005], db[100005];
vector<int> rd[100005];
vector<int> gr[100005];
int lb[100005];
set<int> st[100005];
long long sm[100005];

int main()
{
    ///ios::sync_with_stdio(0);
    ///cin.tie(0);
    ///cout.tie(0);

    cin>>n>>q;
    for(int i=0; i<q; i++)
    {
        cin>>a[i].first>>a[i].second;
    }
    sort(a, a+q);
    for(int i=0; i<q; i++)
    {
        int j=i;
        rd[a[i].first].push_back(brr);
        rd[a[i].second].push_back(brr);
        st[a[i].first].insert(a[i].second);
        while(j<q-1 && a[j+1].first==a[i].first) {j++; rd[a[j].second].push_back(brr); st[a[i].first].insert(a[j].second);}
        db[brr]=1;
        lb[brr]=a[i].first;
        par[brr]=brr;
        brr++;
        i=j;
    }
    for(int i=1; i<=n; i++) lb[i]=i;
    /**
    for(int i=1; i<=n; i++)
    {
        set<int>::iterator it=st[i].begin();
        cout<<i<<endl;
        while(it!=st[i].end()) {cout<<(*it)<<" "; it++;}
        cout<<endl;
    }*/
    long long sum=0;
    for(int i=1; i<=n; i++)
    {
        ///for(int k=0; k<brr; k++)
        ///{
        /**set<int>::iterator it=st[k].begin();
        cout<<k<<endl;
        while(it!=st[k].end()) {cout<<(*it)<<" "; it++;}
        cout<<endl;*/
        ///cout<<loc[k]<<" ";}
        ///cout<<endl;
        int t=rd[i].size();
        if(!t) continue;
        for(int j=0; j<t; j++)
        {
            int v=rd[i][j];
            while(v!=par[v]) v=par[v];
        }
        int tpr=rd[i][0];
        while(tpr!=par[tpr]) tpr=par[tpr];
        for(int j=0; j<t; j++)
        {
            int nr=rd[i][j];
            while(nr!=par[nr]) nr=par[nr];
            if(loc[nr] && lb[loc[nr]]!=lb[i])
            {
            int oprt=lb[i], nprt=lb[loc[nr]], prlab=oprt;
            if(st[oprt].size()>st[nprt].size())
            {
                set<int>::iterator it=st[nprt].begin();
                while(it!=st[nprt].end())
                {

                    st[oprt].insert(*it);
                    it++;
                }
            }
            else
            {
                prlab=lb[nprt];
                set<int>::iterator it=st[oprt].begin();
                while(it!=st[oprt].end())
                {
                    st[nprt].insert(*it);
                    it++;
                }
            }
            lb[i]=prlab;
            }
            if(nr==tpr) continue;
            if(db[tpr]>db[nr]) par[nr]=tpr;
            else if(db[tpr]<db[nr]) {par[tpr]=nr; tpr=nr;}
            else
            {
                par[nr]=tpr;
                db[tpr]++;
            }
        }
        loc[tpr]=i;
        st[lb[i]].insert(i);
        set<int>::iterator it=st[lb[i]].begin();
        while(it!=st[lb[i]].end())
        {
            if((*it)>=i) break;
            st[lb[i]].erase(it);
            it=st[lb[i]].begin();
        }
        sum+=st[lb[i]].size()-1;
        ///cout<<"otg: "<<i<<" "<<sum<<endl;
    }
    cout<<sum<<endl;
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 4 ms 9684 KB Output is correct
3 Correct 7 ms 9932 KB Output is correct
4 Correct 5 ms 9812 KB Output is correct
5 Correct 6 ms 9844 KB Output is correct
6 Correct 6 ms 9848 KB Output is correct
7 Correct 6 ms 9916 KB Output is correct
8 Correct 6 ms 9812 KB Output is correct
9 Correct 4 ms 9660 KB Output is correct
10 Correct 5 ms 9844 KB Output is correct
11 Correct 5 ms 9812 KB Output is correct
12 Correct 6 ms 9812 KB Output is correct
13 Correct 6 ms 9856 KB Output is correct
14 Correct 6 ms 9812 KB Output is correct
15 Correct 5 ms 9624 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 4 ms 9684 KB Output is correct
3 Correct 7 ms 9932 KB Output is correct
4 Correct 5 ms 9812 KB Output is correct
5 Correct 6 ms 9844 KB Output is correct
6 Correct 6 ms 9848 KB Output is correct
7 Correct 6 ms 9916 KB Output is correct
8 Correct 6 ms 9812 KB Output is correct
9 Correct 4 ms 9660 KB Output is correct
10 Correct 5 ms 9844 KB Output is correct
11 Correct 5 ms 9812 KB Output is correct
12 Correct 6 ms 9812 KB Output is correct
13 Correct 6 ms 9856 KB Output is correct
14 Correct 6 ms 9812 KB Output is correct
15 Correct 5 ms 9624 KB Output is correct
16 Correct 66 ms 16552 KB Output is correct
17 Correct 64 ms 16368 KB Output is correct
18 Correct 60 ms 16500 KB Output is correct
19 Correct 62 ms 16436 KB Output is correct
20 Correct 62 ms 16476 KB Output is correct
21 Correct 8 ms 10364 KB Output is correct
22 Correct 65 ms 16828 KB Output is correct
23 Correct 67 ms 16768 KB Output is correct
24 Correct 61 ms 16484 KB Output is correct
25 Correct 62 ms 16484 KB Output is correct
26 Correct 62 ms 16460 KB Output is correct
27 Correct 8 ms 10344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 105 ms 22184 KB Output is correct
2 Correct 96 ms 20820 KB Output is correct
3 Correct 102 ms 20148 KB Output is correct
4 Correct 97 ms 22144 KB Output is correct
5 Correct 101 ms 20964 KB Output is correct
6 Correct 90 ms 20192 KB Output is correct
7 Correct 98 ms 22164 KB Output is correct
8 Correct 100 ms 20912 KB Output is correct
9 Correct 92 ms 20112 KB Output is correct
10 Correct 108 ms 22116 KB Output is correct
11 Correct 100 ms 20844 KB Output is correct
12 Correct 91 ms 20184 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 4 ms 9684 KB Output is correct
3 Correct 7 ms 9932 KB Output is correct
4 Correct 5 ms 9812 KB Output is correct
5 Correct 6 ms 9844 KB Output is correct
6 Correct 6 ms 9848 KB Output is correct
7 Correct 6 ms 9916 KB Output is correct
8 Correct 6 ms 9812 KB Output is correct
9 Correct 4 ms 9660 KB Output is correct
10 Correct 5 ms 9844 KB Output is correct
11 Correct 5 ms 9812 KB Output is correct
12 Correct 6 ms 9812 KB Output is correct
13 Correct 6 ms 9856 KB Output is correct
14 Correct 6 ms 9812 KB Output is correct
15 Correct 5 ms 9624 KB Output is correct
16 Correct 66 ms 16552 KB Output is correct
17 Correct 64 ms 16368 KB Output is correct
18 Correct 60 ms 16500 KB Output is correct
19 Correct 62 ms 16436 KB Output is correct
20 Correct 62 ms 16476 KB Output is correct
21 Correct 8 ms 10364 KB Output is correct
22 Correct 65 ms 16828 KB Output is correct
23 Correct 67 ms 16768 KB Output is correct
24 Correct 61 ms 16484 KB Output is correct
25 Correct 62 ms 16484 KB Output is correct
26 Correct 62 ms 16460 KB Output is correct
27 Correct 8 ms 10344 KB Output is correct
28 Correct 105 ms 22184 KB Output is correct
29 Correct 96 ms 20820 KB Output is correct
30 Correct 102 ms 20148 KB Output is correct
31 Correct 97 ms 22144 KB Output is correct
32 Correct 101 ms 20964 KB Output is correct
33 Correct 90 ms 20192 KB Output is correct
34 Correct 98 ms 22164 KB Output is correct
35 Correct 100 ms 20912 KB Output is correct
36 Correct 92 ms 20112 KB Output is correct
37 Correct 108 ms 22116 KB Output is correct
38 Correct 100 ms 20844 KB Output is correct
39 Correct 91 ms 20184 KB Output is correct
40 Correct 103 ms 28684 KB Output is correct
41 Correct 97 ms 28748 KB Output is correct
42 Correct 115 ms 33888 KB Output is correct
43 Correct 117 ms 33860 KB Output is correct
44 Correct 112 ms 31612 KB Output is correct
45 Correct 106 ms 31728 KB Output is correct
46 Correct 91 ms 20528 KB Output is correct
47 Correct 85 ms 20528 KB Output is correct
48 Correct 90 ms 23080 KB Output is correct
49 Correct 96 ms 23084 KB Output is correct
50 Correct 97 ms 23268 KB Output is correct
51 Correct 99 ms 22040 KB Output is correct
52 Correct 90 ms 21220 KB Output is correct
53 Correct 106 ms 23772 KB Output is correct
54 Correct 107 ms 23852 KB Output is correct
55 Correct 100 ms 23728 KB Output is correct
56 Correct 95 ms 21452 KB Output is correct
57 Correct 70 ms 19460 KB Output is correct