Submission #163196

# Submission time Handle Problem Language Result Execution time Memory
163196 2019-11-11T18:01:33 Z TadijaSebez 우호 조약 체결 (JOI14_friends) C++11
100 / 100
148 ms 12268 KB
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define ll long long
const int N=100050;
bool was[N];
vector<pair<int,int>> edges;
vector<int> E[N];
int p[N];
int Find(int x){ return p[x]==x?x:p[x]=Find(p[x]);}
void Union(int x, int y){ p[Find(x)]=Find(y);}
void DFS(int u, int w)
{
	was[u]=1;
	for(int v:E[u])
	{
		Union(w,v);
		if(!was[v]) DFS(v,w);
	}
}
int cnt[N];
int main()
{
	int n,m;
	scanf("%i %i",&n,&m);
	for(int i=1;i<=n;i++) p[i]=i;
	for(int i=1,u,v;i<=m;i++) scanf("%i %i",&u,&v),E[u].pb(v),edges.pb({u,v});
	for(int i=1;i<=n;i++) if(E[i].size()>1)
	{
		int w=E[i][0];
		for(int v:E[i])
		{
			Union(v,w);
			if(!was[v]) DFS(v,w);
		}
	}
	ll ans=0;
	for(auto e:edges)
	{
		int u,v;tie(u,v)=e;
		if(Find(u)!=Find(v)) ans++;
	}
	for(int i=1;i<=n;i++) cnt[Find(i)]++;
	for(int i=1;i<=n;i++) ans+=(ll)cnt[i]*(cnt[i]-1);
	printf("%lld\n",ans);
	return 0;
}

Compilation message

friends.cpp: In function 'int main()':
friends.cpp:25:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%i %i",&n,&m);
  ~~~~~^~~~~~~~~~~~~~~
friends.cpp:27:59: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  for(int i=1,u,v;i<=m;i++) scanf("%i %i",&u,&v),E[u].pb(v),edges.pb({u,v});
                            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2680 KB Output is correct
2 Correct 4 ms 2680 KB Output is correct
3 Correct 4 ms 2680 KB Output is correct
4 Correct 5 ms 2680 KB Output is correct
5 Correct 4 ms 2676 KB Output is correct
6 Correct 4 ms 2680 KB Output is correct
7 Correct 4 ms 2680 KB Output is correct
8 Correct 5 ms 2680 KB Output is correct
9 Correct 4 ms 2680 KB Output is correct
10 Correct 4 ms 2680 KB Output is correct
11 Correct 4 ms 2680 KB Output is correct
12 Correct 4 ms 2680 KB Output is correct
13 Correct 4 ms 2684 KB Output is correct
14 Correct 4 ms 2684 KB Output is correct
15 Correct 4 ms 2680 KB Output is correct
16 Correct 4 ms 2680 KB Output is correct
17 Correct 4 ms 2680 KB Output is correct
18 Correct 4 ms 2680 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 3064 KB Output is correct
2 Correct 5 ms 2808 KB Output is correct
3 Correct 12 ms 3444 KB Output is correct
4 Correct 52 ms 6988 KB Output is correct
5 Correct 42 ms 5608 KB Output is correct
6 Correct 69 ms 7752 KB Output is correct
7 Correct 5 ms 2808 KB Output is correct
8 Correct 9 ms 3064 KB Output is correct
9 Correct 13 ms 3444 KB Output is correct
10 Correct 23 ms 4208 KB Output is correct
11 Correct 68 ms 7780 KB Output is correct
12 Correct 10 ms 3320 KB Output is correct
13 Correct 6 ms 2936 KB Output is correct
14 Correct 6 ms 2808 KB Output is correct
15 Correct 8 ms 3064 KB Output is correct
16 Correct 16 ms 3572 KB Output is correct
17 Correct 69 ms 7792 KB Output is correct
18 Correct 6 ms 2936 KB Output is correct
19 Correct 6 ms 3064 KB Output is correct
20 Correct 69 ms 7776 KB Output is correct
21 Correct 6 ms 2936 KB Output is correct
22 Correct 6 ms 2936 KB Output is correct
23 Correct 9 ms 3220 KB Output is correct
24 Correct 6 ms 2936 KB Output is correct
25 Correct 6 ms 2936 KB Output is correct
26 Correct 7 ms 2936 KB Output is correct
27 Correct 6 ms 2936 KB Output is correct
28 Correct 6 ms 2936 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 64 ms 7268 KB Output is correct
2 Correct 127 ms 11068 KB Output is correct
3 Correct 139 ms 10980 KB Output is correct
4 Correct 70 ms 7532 KB Output is correct
5 Correct 45 ms 5884 KB Output is correct
6 Correct 107 ms 9828 KB Output is correct
7 Correct 100 ms 10376 KB Output is correct
8 Correct 94 ms 10340 KB Output is correct
9 Correct 59 ms 7788 KB Output is correct
10 Correct 83 ms 9060 KB Output is correct
11 Correct 127 ms 11108 KB Output is correct
12 Correct 148 ms 11932 KB Output is correct
13 Correct 53 ms 8684 KB Output is correct
14 Correct 76 ms 8040 KB Output is correct
15 Correct 31 ms 5848 KB Output is correct
16 Correct 7 ms 3708 KB Output is correct
17 Correct 5 ms 3448 KB Output is correct
18 Correct 135 ms 10404 KB Output is correct
19 Correct 127 ms 10340 KB Output is correct
20 Correct 62 ms 7144 KB Output is correct
21 Correct 30 ms 5616 KB Output is correct
22 Correct 6 ms 3576 KB Output is correct
23 Correct 38 ms 5224 KB Output is correct
24 Correct 67 ms 7404 KB Output is correct
25 Correct 52 ms 8552 KB Output is correct
26 Correct 66 ms 12268 KB Output is correct
27 Correct 144 ms 11844 KB Output is correct
28 Correct 64 ms 8168 KB Output is correct
29 Correct 62 ms 8044 KB Output is correct
30 Correct 143 ms 11204 KB Output is correct
31 Correct 57 ms 7532 KB Output is correct
32 Correct 51 ms 8552 KB Output is correct
33 Correct 47 ms 8552 KB Output is correct
34 Correct 65 ms 7592 KB Output is correct
35 Correct 51 ms 8556 KB Output is correct
36 Correct 61 ms 10092 KB Output is correct
37 Correct 62 ms 8044 KB Output is correct