답안 #199489

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
199489 2020-02-01T15:10:37 Z arnold518 우호 조약 체결 (JOI14_friends) C++14
0 / 100
122 ms 6264 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];

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; }

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(j=1; j<=N; j++)
	for(i=1; i<=N; i++)
	{
		for(auto it : adj[i])
		{
			int u=i, v=it;
			if(Find(u)!=Find(v) && sz[Find(u)]>1) Union(u, v);
		}
	}

	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("\n");
	printf("%lld", ans);
}

Compilation message

friends.cpp: In function 'int main()':
friends.cpp:32:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(j=1; j<adj[i].size(); j++)
            ~^~~~~~~~~~~~~~
friends.cpp:22: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:27: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 6 ms 2680 KB Output is correct
2 Correct 6 ms 2680 KB Output is correct
3 Correct 7 ms 2680 KB Output is correct
4 Correct 6 ms 2680 KB Output is correct
5 Correct 6 ms 2680 KB Output is correct
6 Correct 6 ms 2680 KB Output is correct
7 Correct 7 ms 2680 KB Output is correct
8 Correct 7 ms 2680 KB Output is correct
9 Correct 7 ms 2680 KB Output is correct
10 Correct 6 ms 2680 KB Output is correct
11 Incorrect 7 ms 2680 KB Output isn't correct
12 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 9 ms 2808 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 78 ms 3832 KB Output is correct
2 Incorrect 122 ms 6264 KB Output isn't correct
3 Halted 0 ms 0 KB -