답안 #199501

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
199501 2020-02-01T16:24:05 Z arnold518 우호 조약 체결 (JOI14_friends) C++14
100 / 100
169 ms 18064 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int MAXN = 1e5;

int N, M;
ll ans;
vector<int> adj[MAXN+10], adj2[MAXN+10];

int par[MAXN+10], sz[MAXN+10];
int Find(int x) { return x==par[x] ? x : par[x]=Find(par[x]); }
void Union(int x, int y) { x=Find(x); y=Find(y); sz[y]+=sz[x]; par[x]=y; }

bool vis[MAXN+10];

int main()
{
	int i, j;

	scanf("%d%d", &N, &M);
	for(i=1; i<=N; i++) par[i]=i, sz[i]=1;
	for(i=1; i<=M; i++)
	{
		int u, v;
		scanf("%d%d", &u, &v);
		adj[u].push_back(v);
	}
	for(i=1; i<=N; i++)
	{
		for(j=1; j<adj[i].size(); j++)
		{
			if(Find(adj[i][0])==Find(adj[i][j])) continue;
			Union(adj[i][0], adj[i][j]);
		}
	}

	for(i=1; i<=N; i++)
	{
		for(auto it : adj[i])
		{
			int u=i, v=it;
			u=Find(u), v=Find(v);
			if(u!=v) adj2[u].push_back(v);//, printf("ADJ2 %d %d\n", u, v);
		}
	}
	set<pii, greater<pii>> S;
	vector<pii> V;
	for(i=1; i<=N; i++) if(i==Find(i)) S.insert({sz[i], i});

	while(!S.empty())
	{
		pii t=*S.begin(); S.erase(S.begin());
		if(t.first<=1) continue;
		//for(i=1; i<=N; i++) printf("%d ", Find(i)); printf("\n");
		//printf("NOW %d\n", t.second);
		//for(auto it : S) printf("S %d %d\n", it.first, it.second);
		for(auto nxt : adj2[t.second])
		{
			//printf("NXT / %d %d\n", sz[Find(nxt)], nxt);
			if(Find(t.second)!=Find(nxt)) V.push_back({t.second, nxt});
			if(S.find({sz[Find(nxt)], nxt})==S.end()) continue;
			S.erase({sz[Find(nxt)], nxt});
			S.insert({sz[Find(nxt)]+sz[Find(t.second)], nxt});
		}
	}
	for(auto it : V) if(Find(it.first)!=Find(it.second)) Union(it.first, it.second);

	for(i=1; i<=N; i++)
	{
		for(auto it : adj[i])
		{
			int u=i, v=it;
			if(Find(u)!=Find(v)) ans++;
		}
	}

	for(i=1; i<=N; i++) if(i==Find(i)) ans+=(ll)sz[i]*(sz[i]-1);
	//for(i=1; i<=N; i++) printf("%d ", Find(i));
	printf("%lld", ans);
}

Compilation message

friends.cpp: In function 'int main()':
friends.cpp:34:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(j=1; j<adj[i].size(); j++)
            ~^~~~~~~~~~~~~~
friends.cpp:24:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d", &N, &M);
  ~~~~~^~~~~~~~~~~~~~~~
friends.cpp:29:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d", &u, &v);
   ~~~~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 4984 KB Output is correct
2 Correct 8 ms 4984 KB Output is correct
3 Correct 8 ms 4984 KB Output is correct
4 Correct 8 ms 4984 KB Output is correct
5 Correct 8 ms 4984 KB Output is correct
6 Correct 8 ms 4984 KB Output is correct
7 Correct 8 ms 4984 KB Output is correct
8 Correct 9 ms 4984 KB Output is correct
9 Correct 9 ms 4984 KB Output is correct
10 Correct 8 ms 4984 KB Output is correct
11 Correct 8 ms 4984 KB Output is correct
12 Correct 8 ms 4984 KB Output is correct
13 Correct 8 ms 4984 KB Output is correct
14 Correct 8 ms 5112 KB Output is correct
15 Correct 8 ms 5112 KB Output is correct
16 Correct 8 ms 4984 KB Output is correct
17 Correct 8 ms 4984 KB Output is correct
18 Correct 8 ms 4984 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 5368 KB Output is correct
2 Correct 10 ms 5404 KB Output is correct
3 Correct 17 ms 5368 KB Output is correct
4 Correct 58 ms 6008 KB Output is correct
5 Correct 51 ms 6904 KB Output is correct
6 Correct 75 ms 8440 KB Output is correct
7 Correct 9 ms 5112 KB Output is correct
8 Correct 13 ms 5240 KB Output is correct
9 Correct 18 ms 5496 KB Output is correct
10 Correct 28 ms 6012 KB Output is correct
11 Correct 73 ms 8440 KB Output is correct
12 Correct 15 ms 5368 KB Output is correct
13 Correct 13 ms 5496 KB Output is correct
14 Correct 12 ms 5496 KB Output is correct
15 Correct 13 ms 5368 KB Output is correct
16 Correct 20 ms 5624 KB Output is correct
17 Correct 77 ms 8312 KB Output is correct
18 Correct 11 ms 5624 KB Output is correct
19 Correct 12 ms 5624 KB Output is correct
20 Correct 76 ms 8312 KB Output is correct
21 Correct 12 ms 5496 KB Output is correct
22 Correct 12 ms 5496 KB Output is correct
23 Correct 13 ms 5368 KB Output is correct
24 Correct 13 ms 5496 KB Output is correct
25 Correct 12 ms 5624 KB Output is correct
26 Correct 13 ms 5500 KB Output is correct
27 Correct 11 ms 5624 KB Output is correct
28 Correct 12 ms 5752 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 68 ms 6136 KB Output is correct
2 Correct 156 ms 10488 KB Output is correct
3 Correct 164 ms 12792 KB Output is correct
4 Correct 130 ms 13688 KB Output is correct
5 Correct 83 ms 13304 KB Output is correct
6 Correct 118 ms 9720 KB Output is correct
7 Correct 117 ms 12920 KB Output is correct
8 Correct 117 ms 12920 KB Output is correct
9 Correct 105 ms 13684 KB Output is correct
10 Correct 113 ms 12920 KB Output is correct
11 Correct 158 ms 12980 KB Output is correct
12 Correct 149 ms 13048 KB Output is correct
13 Correct 101 ms 17912 KB Output is correct
14 Correct 78 ms 8592 KB Output is correct
15 Correct 77 ms 13304 KB Output is correct
16 Correct 37 ms 10744 KB Output is correct
17 Correct 35 ms 10488 KB Output is correct
18 Correct 151 ms 12024 KB Output is correct
19 Correct 144 ms 12024 KB Output is correct
20 Correct 106 ms 12660 KB Output is correct
21 Correct 87 ms 12664 KB Output is correct
22 Correct 36 ms 10488 KB Output is correct
23 Correct 40 ms 6520 KB Output is correct
24 Correct 102 ms 11384 KB Output is correct
25 Correct 99 ms 17916 KB Output is correct
26 Correct 129 ms 17912 KB Output is correct
27 Correct 169 ms 13304 KB Output is correct
28 Correct 117 ms 15604 KB Output is correct
29 Correct 126 ms 15604 KB Output is correct
30 Correct 160 ms 12700 KB Output is correct
31 Correct 149 ms 13300 KB Output is correct
32 Correct 98 ms 17912 KB Output is correct
33 Correct 95 ms 17912 KB Output is correct
34 Correct 129 ms 14836 KB Output is correct
35 Correct 103 ms 17912 KB Output is correct
36 Correct 121 ms 18064 KB Output is correct
37 Correct 114 ms 15604 KB Output is correct