Submission #278591

# Submission time Handle Problem Language Result Execution time Memory
278591 2020-08-21T15:09:56 Z MKopchev Making Friends on Joitter is Fun (JOI20_joitter2) C++14
17 / 100
5000 ms 231680 KB
#include<bits/stdc++.h>
using namespace std;
const int nmax=1e5+42;

int n,m;
int parent[nmax],SZ[nmax];

int root(int node)
{
    if(node==parent[node])return node;
    parent[node]=root(parent[node]);
    return parent[node];
}

set< pair<int/*from*/,int/*to component*/> > all[nmax];
set< int > component[nmax];
set< pair<int/*from*/,int/*to*/> > edges_comp;

long long output=0;

int in[nmax];

long long score(int node)
{
    node=root(node);

    return 1LL*SZ[node]*(SZ[node]-1)/2*2+1LL*in[node]*SZ[node];
}
void add(int u,int v,bool special)
{
    /*
    cout<<"\n\n\n"<<endl;

    cout<<"add "<<u<<" "<<v<<" "<<output<<" "<<special<<endl;

    cout<<"edges_comp"<<endl;
    for(auto k:edges_comp)cout<<k.first<<" "<<k.second<<endl;

    for(int i=1;i<=n;i++)
    {
        cout<<i<<" -> "<<in[i]<<" "<<root(i)<<" "<<SZ[root(i)]<<endl;
        cout<<"all: "<<endl;
        for(auto j:all[i])cout<<j.first<<" "<<j.second<<endl;
        cout<<"component: ";for(auto k:component[i])cout<<k<<" ";cout<<endl;

        cout<<"---"<<endl;
    }
    */

    int u_root=root(u);
    int v_root=root(v);

    if(u_root==v_root)return;

    if(special==0&&all[u_root].count({u,v_root}))return;

    if(edges_comp.count({v_root,u_root})==0)//do not merge
    {
        if(all[u_root].count({u,v_root})==0)
        {
            output+=SZ[v_root];
            in[v_root]++;
        }
        all[u_root].insert({u,v_root});
        all[v_root].insert({u,v_root});

        edges_comp.insert({u_root,v_root});
        return;
    }
    /*
    if(SZ[u_root]<SZ[v_root])
    {
        swap(u_root,v_root);
        swap(u,v);
    }
    */

    edges_comp.erase({u_root,v_root});
    edges_comp.erase({v_root,u_root});

    //cout<<u_root<<" "<<u<<" "<<v_root<<" "<<v<<endl;

    //merge
    output=output-score(u_root)-score(v_root);

    //cout<<"after sub "<<output<<endl;

    set< pair<int/*from*/,int/*to component*/> > mem_all=all[v_root];

    for(auto p:mem_all)
    {
        int where=-1;

        pair<int,int> k=p;

        //cout<<"moving "<<k.first<<" "<<k.second<<endl;

        if(k.second==v_root&&component[u_root].count(k.first))
        {
            all[u_root].erase({k.first,v_root});
            all[v_root].erase({k.first,v_root});

            in[v_root]--;

            where=1;
        }
        else if(k.second==v_root)
        {
            all[root(k.first)].erase(k);

            k.second=u_root;

            if(all[u_root].count(k)==0)in[u_root]++;

            all[u_root].insert(k);
            all[root(k.first)].insert(k);

            edges_comp.insert({root(k.first),u_root});
            edges_comp.erase({root(k.first),v_root});

            where=2;
        }

        else if(component[u_root].count(k.second)==0)
        {
            //if(all[u_root].count({u_root,root(k.second)})==0)output+=SZ[u_root];in[root(k.second)]++;

            all[u_root].insert({k.first,root(k.second)});
            all[root(k.second)].insert({k.first,root(k.second)});

            edges_comp.insert({u_root,root(k.second)});

            /*
            cout<<"Wrong "<<endl;
            system("pause");
            */

            where=3;
        }
        else
        {
            in[u_root]--;

            all[u_root].erase({k.first,u_root});
            all[v_root].erase({k.first,u_root});

            where=4;
        }

        //cout<<"where= "<<where<<" "<<in[u_root]<<endl;
    }

    all[v_root]={};
    in[v_root]=0;

    SZ[u_root]+=SZ[v_root];
    parent[v_root]=u_root;

    output+=score(u_root);

    //cout<<"after add "<<output<<endl;

    for(auto k:component[v_root])
        component[u_root].insert(k);
    component[v_root]={};

    for(auto k:mem_all)
        add(k.first,k.second,1);
}
int main()
{
    scanf("%i%i",&n,&m);

    for(int i=1;i<=n;i++)parent[i]=i,SZ[i]=1,component[i]={i};

    for(int i=1;i<=m;i++)
    {
        int u,v;

        scanf("%i%i",&u,&v);

        add(u,v,0);

        printf("%lld\n",output);
    }
    return 0;
}

Compilation message

joitter2.cpp: In function 'void add(int, int, bool)':
joitter2.cpp:92:13: warning: variable 'where' set but not used [-Wunused-but-set-variable]
   92 |         int where=-1;
      |             ^~~~~
joitter2.cpp: In function 'int main()':
joitter2.cpp:172:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  172 |     scanf("%i%i",&n,&m);
      |     ~~~~~^~~~~~~~~~~~~~
joitter2.cpp:180:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  180 |         scanf("%i%i",&u,&v);
      |         ~~~~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9728 KB Output is correct
2 Correct 7 ms 9728 KB Output is correct
3 Correct 7 ms 9728 KB Output is correct
4 Correct 8 ms 9728 KB Output is correct
5 Correct 7 ms 9728 KB Output is correct
6 Correct 7 ms 9728 KB Output is correct
7 Correct 9 ms 9856 KB Output is correct
8 Correct 9 ms 9856 KB Output is correct
9 Correct 9 ms 9856 KB Output is correct
10 Correct 8 ms 9728 KB Output is correct
11 Correct 6 ms 9728 KB Output is correct
12 Correct 7 ms 9728 KB Output is correct
13 Correct 7 ms 9728 KB Output is correct
14 Correct 7 ms 9728 KB Output is correct
15 Correct 8 ms 9728 KB Output is correct
16 Correct 7 ms 9728 KB Output is correct
17 Correct 7 ms 9728 KB Output is correct
18 Correct 7 ms 9728 KB Output is correct
19 Correct 8 ms 9728 KB Output is correct
20 Correct 10 ms 9728 KB Output is correct
21 Correct 9 ms 9888 KB Output is correct
22 Correct 7 ms 9728 KB Output is correct
23 Correct 8 ms 9856 KB Output is correct
24 Correct 8 ms 9728 KB Output is correct
25 Correct 9 ms 9856 KB Output is correct
26 Correct 7 ms 9728 KB Output is correct
27 Correct 7 ms 9728 KB Output is correct
28 Correct 8 ms 9728 KB Output is correct
29 Correct 7 ms 9728 KB Output is correct
30 Correct 6 ms 9728 KB Output is correct
31 Correct 7 ms 9728 KB Output is correct
32 Correct 7 ms 9728 KB Output is correct
33 Correct 8 ms 9856 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9728 KB Output is correct
2 Correct 7 ms 9728 KB Output is correct
3 Correct 7 ms 9728 KB Output is correct
4 Correct 8 ms 9728 KB Output is correct
5 Correct 7 ms 9728 KB Output is correct
6 Correct 7 ms 9728 KB Output is correct
7 Correct 9 ms 9856 KB Output is correct
8 Correct 9 ms 9856 KB Output is correct
9 Correct 9 ms 9856 KB Output is correct
10 Correct 8 ms 9728 KB Output is correct
11 Correct 6 ms 9728 KB Output is correct
12 Correct 7 ms 9728 KB Output is correct
13 Correct 7 ms 9728 KB Output is correct
14 Correct 7 ms 9728 KB Output is correct
15 Correct 8 ms 9728 KB Output is correct
16 Correct 7 ms 9728 KB Output is correct
17 Correct 7 ms 9728 KB Output is correct
18 Correct 7 ms 9728 KB Output is correct
19 Correct 8 ms 9728 KB Output is correct
20 Correct 10 ms 9728 KB Output is correct
21 Correct 9 ms 9888 KB Output is correct
22 Correct 7 ms 9728 KB Output is correct
23 Correct 8 ms 9856 KB Output is correct
24 Correct 8 ms 9728 KB Output is correct
25 Correct 9 ms 9856 KB Output is correct
26 Correct 7 ms 9728 KB Output is correct
27 Correct 7 ms 9728 KB Output is correct
28 Correct 8 ms 9728 KB Output is correct
29 Correct 7 ms 9728 KB Output is correct
30 Correct 6 ms 9728 KB Output is correct
31 Correct 7 ms 9728 KB Output is correct
32 Correct 7 ms 9728 KB Output is correct
33 Correct 8 ms 9856 KB Output is correct
34 Correct 15 ms 10112 KB Output is correct
35 Correct 657 ms 55164 KB Output is correct
36 Correct 3334 ms 217940 KB Output is correct
37 Correct 3318 ms 231680 KB Output is correct
38 Correct 3056 ms 207004 KB Output is correct
39 Correct 234 ms 9984 KB Output is correct
40 Correct 14 ms 10112 KB Output is correct
41 Correct 14 ms 10112 KB Output is correct
42 Correct 219 ms 10104 KB Output is correct
43 Correct 968 ms 26360 KB Output is correct
44 Correct 904 ms 25720 KB Output is correct
45 Correct 214 ms 9976 KB Output is correct
46 Correct 232 ms 10132 KB Output is correct
47 Correct 51 ms 10488 KB Output is correct
48 Correct 54 ms 10488 KB Output is correct
49 Correct 453 ms 37880 KB Output is correct
50 Correct 3275 ms 228324 KB Output is correct
51 Correct 1452 ms 25592 KB Output is correct
52 Correct 1821 ms 136660 KB Output is correct
53 Correct 586 ms 41976 KB Output is correct
54 Correct 2355 ms 181368 KB Output is correct
55 Correct 724 ms 53880 KB Output is correct
56 Correct 733 ms 52472 KB Output is correct
57 Correct 712 ms 45944 KB Output is correct
58 Correct 717 ms 46200 KB Output is correct
59 Correct 13 ms 9984 KB Output is correct
60 Correct 129 ms 14968 KB Output is correct
61 Correct 186 ms 12152 KB Output is correct
62 Correct 2601 ms 192904 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9728 KB Output is correct
2 Correct 7 ms 9728 KB Output is correct
3 Correct 7 ms 9728 KB Output is correct
4 Correct 8 ms 9728 KB Output is correct
5 Correct 7 ms 9728 KB Output is correct
6 Correct 7 ms 9728 KB Output is correct
7 Correct 9 ms 9856 KB Output is correct
8 Correct 9 ms 9856 KB Output is correct
9 Correct 9 ms 9856 KB Output is correct
10 Correct 8 ms 9728 KB Output is correct
11 Correct 6 ms 9728 KB Output is correct
12 Correct 7 ms 9728 KB Output is correct
13 Correct 7 ms 9728 KB Output is correct
14 Correct 7 ms 9728 KB Output is correct
15 Correct 8 ms 9728 KB Output is correct
16 Correct 7 ms 9728 KB Output is correct
17 Correct 7 ms 9728 KB Output is correct
18 Correct 7 ms 9728 KB Output is correct
19 Correct 8 ms 9728 KB Output is correct
20 Correct 10 ms 9728 KB Output is correct
21 Correct 9 ms 9888 KB Output is correct
22 Correct 7 ms 9728 KB Output is correct
23 Correct 8 ms 9856 KB Output is correct
24 Correct 8 ms 9728 KB Output is correct
25 Correct 9 ms 9856 KB Output is correct
26 Correct 7 ms 9728 KB Output is correct
27 Correct 7 ms 9728 KB Output is correct
28 Correct 8 ms 9728 KB Output is correct
29 Correct 7 ms 9728 KB Output is correct
30 Correct 6 ms 9728 KB Output is correct
31 Correct 7 ms 9728 KB Output is correct
32 Correct 7 ms 9728 KB Output is correct
33 Correct 8 ms 9856 KB Output is correct
34 Correct 15 ms 10112 KB Output is correct
35 Correct 657 ms 55164 KB Output is correct
36 Correct 3334 ms 217940 KB Output is correct
37 Correct 3318 ms 231680 KB Output is correct
38 Correct 3056 ms 207004 KB Output is correct
39 Correct 234 ms 9984 KB Output is correct
40 Correct 14 ms 10112 KB Output is correct
41 Correct 14 ms 10112 KB Output is correct
42 Correct 219 ms 10104 KB Output is correct
43 Correct 968 ms 26360 KB Output is correct
44 Correct 904 ms 25720 KB Output is correct
45 Correct 214 ms 9976 KB Output is correct
46 Correct 232 ms 10132 KB Output is correct
47 Correct 51 ms 10488 KB Output is correct
48 Correct 54 ms 10488 KB Output is correct
49 Correct 453 ms 37880 KB Output is correct
50 Correct 3275 ms 228324 KB Output is correct
51 Correct 1452 ms 25592 KB Output is correct
52 Correct 1821 ms 136660 KB Output is correct
53 Correct 586 ms 41976 KB Output is correct
54 Correct 2355 ms 181368 KB Output is correct
55 Correct 724 ms 53880 KB Output is correct
56 Correct 733 ms 52472 KB Output is correct
57 Correct 712 ms 45944 KB Output is correct
58 Correct 717 ms 46200 KB Output is correct
59 Correct 13 ms 9984 KB Output is correct
60 Correct 129 ms 14968 KB Output is correct
61 Correct 186 ms 12152 KB Output is correct
62 Correct 2601 ms 192904 KB Output is correct
63 Correct 1104 ms 63440 KB Output is correct
64 Correct 1098 ms 63508 KB Output is correct
65 Correct 1097 ms 63288 KB Output is correct
66 Execution timed out 5073 ms 16712 KB Time limit exceeded
67 Halted 0 ms 0 KB -