Submission #389749

# Submission time Handle Problem Language Result Execution time Memory
389749 2021-04-14T12:43:47 Z ogibogi2004 Making Friends on Joitter is Fun (JOI20_joitter2) C++14
17 / 100
5000 ms 22892 KB
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int MAXN=1e5+6;
int n,m;
int par[MAXN];
ll sz[MAXN];
set<int>edges_in[MAXN];
set<int>edges_out[MAXN];
set<int>comp[MAXN];
ll ans=0;
int getRoot(int u)
{
	if(u==par[u])return u;
	return par[u]=getRoot(par[u]);
}
ll cnt(int u)
{
	return sz[u]*(sz[u]-1ll)+edges_in[u].size()*sz[u];
}
stack<pair<int,int> >merges;
void merge(int a,int b)
{
	a=getRoot(a);b=getRoot(b);
	if(a==b)return;
	if(comp[b].size()+edges_in[b].size()+edges_out[b].size()>comp[a].size()+edges_in[a].size()+edges_out[a].size())swap(a,b);
	for(auto xd:edges_in[b])
	{
		int t=getRoot(xd);
		if(t==a)
		{
			continue;
		}
		edges_out[t].erase(b);
		edges_out[t].insert(a);
		edges_in[a].insert(xd);
		if(edges_out[a].find(t)!=edges_out[a].end())
		{
			merges.push({xd,a});
		}
	}
	for(auto xd:edges_out[b])
	{
		int t=getRoot(xd);
		if(xd==a)
		{
			continue;
		}
		edges_out[a].insert(xd);
		//edges_in[t].erase(b);
		//edges_in[t].insert(a);
		if(edges_out[xd].find(a)!=edges_out[xd].end())
		{
			merges.push({xd,a});
		}
	}
	edges_in[a].erase(b);
	edges_out[a].erase(b);
	for(auto xd:comp[b])
	{
		edges_in[a].erase(xd);
		comp[a].insert(xd);
	}
	par[b]=a;
	sz[a]+=sz[b];
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	cin>>n>>m;
	for(int i=1;i<=n;i++)
	{
		par[i]=i;comp[i].insert(i);
		sz[i]=1;
	}
	for(int i=0;i<m;i++)
	{
		int a,b,a1,b1;
		cin>>a>>b;a1=a;b1=b;
		a=getRoot(a);
		b=getRoot(b);
		if(a==b)
		{
			//cout<<ans<<endl;
			//continue;
		}
		else
		{
			if(edges_out[b].find(a)!=edges_out[b].end())
			{
				merges.push({a,b});
				edges_in[b].insert(a1);
				edges_out[a].insert(b);
				int x,y;
				while(!merges.empty())
				{
					tie(x,y)=merges.top();merges.pop();
					x=getRoot(x);y=getRoot(y);
					if(x!=y)
					{
						ans-=cnt(x);
						ans-=cnt(y);
						merge(x,y);
						ans+=cnt(getRoot(x));
					}
					//ans+=cnt(getRoot(y));
				}
			}
			else if(edges_out[b].find(a)==edges_out[b].end())
			{
				ans-=cnt(b);
				edges_in[b].insert(a1);
				edges_out[a].insert(b);
				ans+=cnt(b);
			}
			/*for(int j=1;j<=n;j++)
			{
				cout<<getRoot(j)<<" ";
			}
			cout<<endl;
			for(int j=1;j<=n;j++)
			{
				cout<<cnt(getRoot(j))<<" ";
			}
			cout<<endl;
			cout<<ans<<endl;
			*/
		}
		/*for(int j=1;j<=n;j++)cout<<getRoot(j)<<" ";
		cout<<endl;
		for(int j=1;j<=n;j++)cout<<cnt(getRoot(j))<<" ";
		cout<<endl;*/
		ll sum=0;
		for(int j=1;j<=n;j++)
		{
			if(j==getRoot(j))
			{
				sum+=cnt(j);
			}
		}
		cout<<sum<<endl;
		/*cout<<"**\n";
		for(int j=1;j<=n;j++)
		{
			cout<<getRoot(j)<<":\n";
			for(auto xd:edges_in[getRoot(j)])
			{
				cout<<xd<<" ";
			}
			cout<<endl;
		}*/
	}
return 0;
}

Compilation message

joitter2.cpp: In function 'void merge(int, int)':
joitter2.cpp:44:7: warning: unused variable 't' [-Wunused-variable]
   44 |   int t=getRoot(xd);
      |       ^
joitter2.cpp: In function 'int main()':
joitter2.cpp:80:14: warning: variable 'b1' set but not used [-Wunused-but-set-variable]
   80 |   int a,b,a1,b1;
      |              ^~
# Verdict Execution time Memory Grader output
1 Correct 9 ms 14416 KB Output is correct
2 Correct 9 ms 14412 KB Output is correct
3 Correct 9 ms 14376 KB Output is correct
4 Correct 9 ms 14304 KB Output is correct
5 Correct 9 ms 14372 KB Output is correct
6 Correct 9 ms 14384 KB Output is correct
7 Correct 14 ms 14412 KB Output is correct
8 Correct 14 ms 14424 KB Output is correct
9 Correct 15 ms 14440 KB Output is correct
10 Correct 10 ms 14416 KB Output is correct
11 Correct 10 ms 14416 KB Output is correct
12 Correct 10 ms 14308 KB Output is correct
13 Correct 9 ms 14412 KB Output is correct
14 Correct 10 ms 14316 KB Output is correct
15 Correct 10 ms 14412 KB Output is correct
16 Correct 11 ms 14412 KB Output is correct
17 Correct 10 ms 14412 KB Output is correct
18 Correct 9 ms 14308 KB Output is correct
19 Correct 10 ms 14412 KB Output is correct
20 Correct 10 ms 14412 KB Output is correct
21 Correct 16 ms 14432 KB Output is correct
22 Correct 10 ms 14416 KB Output is correct
23 Correct 13 ms 14420 KB Output is correct
24 Correct 10 ms 14412 KB Output is correct
25 Correct 13 ms 14416 KB Output is correct
26 Correct 9 ms 14412 KB Output is correct
27 Correct 10 ms 14412 KB Output is correct
28 Correct 10 ms 14412 KB Output is correct
29 Correct 10 ms 14412 KB Output is correct
30 Correct 10 ms 14408 KB Output is correct
31 Correct 13 ms 14344 KB Output is correct
32 Correct 10 ms 14320 KB Output is correct
33 Correct 15 ms 14412 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 14416 KB Output is correct
2 Correct 9 ms 14412 KB Output is correct
3 Correct 9 ms 14376 KB Output is correct
4 Correct 9 ms 14304 KB Output is correct
5 Correct 9 ms 14372 KB Output is correct
6 Correct 9 ms 14384 KB Output is correct
7 Correct 14 ms 14412 KB Output is correct
8 Correct 14 ms 14424 KB Output is correct
9 Correct 15 ms 14440 KB Output is correct
10 Correct 10 ms 14416 KB Output is correct
11 Correct 10 ms 14416 KB Output is correct
12 Correct 10 ms 14308 KB Output is correct
13 Correct 9 ms 14412 KB Output is correct
14 Correct 10 ms 14316 KB Output is correct
15 Correct 10 ms 14412 KB Output is correct
16 Correct 11 ms 14412 KB Output is correct
17 Correct 10 ms 14412 KB Output is correct
18 Correct 9 ms 14308 KB Output is correct
19 Correct 10 ms 14412 KB Output is correct
20 Correct 10 ms 14412 KB Output is correct
21 Correct 16 ms 14432 KB Output is correct
22 Correct 10 ms 14416 KB Output is correct
23 Correct 13 ms 14420 KB Output is correct
24 Correct 10 ms 14412 KB Output is correct
25 Correct 13 ms 14416 KB Output is correct
26 Correct 9 ms 14412 KB Output is correct
27 Correct 10 ms 14412 KB Output is correct
28 Correct 10 ms 14412 KB Output is correct
29 Correct 10 ms 14412 KB Output is correct
30 Correct 10 ms 14408 KB Output is correct
31 Correct 13 ms 14344 KB Output is correct
32 Correct 10 ms 14320 KB Output is correct
33 Correct 15 ms 14412 KB Output is correct
34 Correct 31 ms 14568 KB Output is correct
35 Correct 1588 ms 19764 KB Output is correct
36 Correct 2639 ms 21976 KB Output is correct
37 Correct 2594 ms 21952 KB Output is correct
38 Correct 2714 ms 21736 KB Output is correct
39 Correct 60 ms 14836 KB Output is correct
40 Correct 58 ms 15156 KB Output is correct
41 Correct 47 ms 15096 KB Output is correct
42 Correct 52 ms 14752 KB Output is correct
43 Correct 48 ms 14788 KB Output is correct
44 Correct 50 ms 14796 KB Output is correct
45 Correct 54 ms 14784 KB Output is correct
46 Correct 52 ms 14844 KB Output is correct
47 Correct 47 ms 15052 KB Output is correct
48 Correct 48 ms 14976 KB Output is correct
49 Correct 107 ms 15428 KB Output is correct
50 Correct 2653 ms 22052 KB Output is correct
51 Correct 106 ms 15052 KB Output is correct
52 Correct 2751 ms 20880 KB Output is correct
53 Correct 92 ms 15280 KB Output is correct
54 Correct 2635 ms 21424 KB Output is correct
55 Correct 41 ms 15044 KB Output is correct
56 Correct 34 ms 14932 KB Output is correct
57 Correct 36 ms 14940 KB Output is correct
58 Correct 32 ms 15020 KB Output is correct
59 Correct 57 ms 15188 KB Output is correct
60 Correct 2687 ms 20228 KB Output is correct
61 Correct 51 ms 15044 KB Output is correct
62 Correct 2589 ms 21636 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 14416 KB Output is correct
2 Correct 9 ms 14412 KB Output is correct
3 Correct 9 ms 14376 KB Output is correct
4 Correct 9 ms 14304 KB Output is correct
5 Correct 9 ms 14372 KB Output is correct
6 Correct 9 ms 14384 KB Output is correct
7 Correct 14 ms 14412 KB Output is correct
8 Correct 14 ms 14424 KB Output is correct
9 Correct 15 ms 14440 KB Output is correct
10 Correct 10 ms 14416 KB Output is correct
11 Correct 10 ms 14416 KB Output is correct
12 Correct 10 ms 14308 KB Output is correct
13 Correct 9 ms 14412 KB Output is correct
14 Correct 10 ms 14316 KB Output is correct
15 Correct 10 ms 14412 KB Output is correct
16 Correct 11 ms 14412 KB Output is correct
17 Correct 10 ms 14412 KB Output is correct
18 Correct 9 ms 14308 KB Output is correct
19 Correct 10 ms 14412 KB Output is correct
20 Correct 10 ms 14412 KB Output is correct
21 Correct 16 ms 14432 KB Output is correct
22 Correct 10 ms 14416 KB Output is correct
23 Correct 13 ms 14420 KB Output is correct
24 Correct 10 ms 14412 KB Output is correct
25 Correct 13 ms 14416 KB Output is correct
26 Correct 9 ms 14412 KB Output is correct
27 Correct 10 ms 14412 KB Output is correct
28 Correct 10 ms 14412 KB Output is correct
29 Correct 10 ms 14412 KB Output is correct
30 Correct 10 ms 14408 KB Output is correct
31 Correct 13 ms 14344 KB Output is correct
32 Correct 10 ms 14320 KB Output is correct
33 Correct 15 ms 14412 KB Output is correct
34 Correct 31 ms 14568 KB Output is correct
35 Correct 1588 ms 19764 KB Output is correct
36 Correct 2639 ms 21976 KB Output is correct
37 Correct 2594 ms 21952 KB Output is correct
38 Correct 2714 ms 21736 KB Output is correct
39 Correct 60 ms 14836 KB Output is correct
40 Correct 58 ms 15156 KB Output is correct
41 Correct 47 ms 15096 KB Output is correct
42 Correct 52 ms 14752 KB Output is correct
43 Correct 48 ms 14788 KB Output is correct
44 Correct 50 ms 14796 KB Output is correct
45 Correct 54 ms 14784 KB Output is correct
46 Correct 52 ms 14844 KB Output is correct
47 Correct 47 ms 15052 KB Output is correct
48 Correct 48 ms 14976 KB Output is correct
49 Correct 107 ms 15428 KB Output is correct
50 Correct 2653 ms 22052 KB Output is correct
51 Correct 106 ms 15052 KB Output is correct
52 Correct 2751 ms 20880 KB Output is correct
53 Correct 92 ms 15280 KB Output is correct
54 Correct 2635 ms 21424 KB Output is correct
55 Correct 41 ms 15044 KB Output is correct
56 Correct 34 ms 14932 KB Output is correct
57 Correct 36 ms 14940 KB Output is correct
58 Correct 32 ms 15020 KB Output is correct
59 Correct 57 ms 15188 KB Output is correct
60 Correct 2687 ms 20228 KB Output is correct
61 Correct 51 ms 15044 KB Output is correct
62 Correct 2589 ms 21636 KB Output is correct
63 Execution timed out 5087 ms 22892 KB Time limit exceeded
64 Halted 0 ms 0 KB -