Submission #142821

# Submission time Handle Problem Language Result Execution time Memory
142821 2019-08-11T10:33:45 Z cheetose 우호 조약 체결 (JOI14_friends) C++11
100 / 100
116 ms 11900 KB
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<vector>
#include<queue>
#include<bitset>
#include<string>
#include<stack>
#include<set>
#include<unordered_set>
#include<map>
#include<unordered_map>
#include<cstring>
#include<complex>
#include<cmath>
#include<iomanip>
#include<numeric>
#include<algorithm>
#include<list>
#include<functional>
#include<cassert>
#define mp make_pair
#define pb push_back
#define X first
#define Y second
#define y0 y12
#define y1 y22
#define INF 987654321987654321
#define PI 3.141592653589793238462643383279502884
#define fup(i,a,b,c) for(int (i)=(a);(i)<=(b);(i)+=(c))
#define fdn(i,a,b,c) for(int (i)=(a);(i)>=(b);(i)-=(c))
#define MEM0(a) memset((a),0,sizeof(a));
#define MEM_1(a) memset((a),-1,sizeof(a));
#define ALL(a) a.begin(),a.end()
#define SYNC ios_base::sync_with_stdio(false);cin.tie(0)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int, int> Pi;
typedef pair<ll, ll> Pll;
typedef pair<ld, ld> Pd;
typedef vector<int> Vi;
typedef vector<ll> Vll;
typedef vector<double> Vd;
typedef vector<Pi> VPi;
typedef vector<Pll> VPll;
typedef vector<Pd> VPd;
typedef tuple<int, int, int> iii;
typedef tuple<int,int,int,int> iiii;
typedef tuple<ll, ll, ll> LLL;
typedef vector<iii> Viii;
typedef vector<LLL> VLLL;
typedef complex<double> base;
const ll MOD = 1000000007;
ll POW(ll a, ll b, ll MMM = MOD) {ll ret=1; for(;b;b>>=1,a=(a*a)%MMM)if(b&1)ret=(ret*a)% MMM; return ret; }
ll gcd(ll a, ll b) { return b ? gcd(b, a%b) : a; }
ll lcm(ll a, ll b) { if (a == 0 || b == 0)return a + b; return a*(b / gcd(a, b)); }
int dx[] = { 0,1,0,-1,1,1,-1,-1 }, dy[] = { 1,0,-1,0,1,-1,1,-1 };

int parent[100001];
int find(int a)
{
	if (parent[a] < 0)return a;
	return parent[a] = find(parent[a]);
}
void merge(int a, int b)
{
	a = find(a), b = find(b);
	if (a != b)
	{
		parent[a] += parent[b];
		parent[b] = a;
	}
}
int n,m;
Vi v[100001];
bool chk[100001];
void dfs(int N)
{
	while(!v[N].empty())
	{
		int next=v[N].back();
		merge(N,next);
		v[N].pop_back();
		dfs(next);
	}
}
int main() {
	MEM_1(parent);
	scanf("%d%d",&n,&m);
	fup(i,1,m,1)
	{
		int x,y;
		scanf("%d%d",&x,&y);
		v[x].pb(y);
	}
	fup(i,1,n,1)
	{
		if(v[i].size()>=2)
		{
			for(int next:v[i])dfs(next);
		}
		fup(j,0,(int)v[i].size()-2,1)merge(v[i][j],v[i][j+1]);
	}
	ll ans=0;
	fup(i,1,n,1)
	{
		int t=find(i);
		if(chk[t])continue;
		int sz=-parent[t];
		if(sz>=2)ans+=1LL*sz*(sz-1);
		else ans+=v[t].size();
		chk[t]=1;
	}
	printf("%lld",ans);
}

Compilation message

friends.cpp: In function 'int main()':
friends.cpp:93: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:97:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d",&x,&y);
   ~~~~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 3192 KB Output is correct
2 Correct 4 ms 3064 KB Output is correct
3 Correct 5 ms 3064 KB Output is correct
4 Correct 4 ms 3064 KB Output is correct
5 Correct 4 ms 3104 KB Output is correct
6 Correct 4 ms 3064 KB Output is correct
7 Correct 5 ms 3064 KB Output is correct
8 Correct 5 ms 3192 KB Output is correct
9 Correct 5 ms 3064 KB Output is correct
10 Correct 5 ms 3064 KB Output is correct
11 Correct 4 ms 3064 KB Output is correct
12 Correct 4 ms 3064 KB Output is correct
13 Correct 4 ms 3064 KB Output is correct
14 Correct 4 ms 3068 KB Output is correct
15 Correct 4 ms 3064 KB Output is correct
16 Correct 4 ms 3064 KB Output is correct
17 Correct 5 ms 3192 KB Output is correct
18 Correct 4 ms 3064 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 3320 KB Output is correct
2 Correct 6 ms 3192 KB Output is correct
3 Correct 13 ms 3704 KB Output is correct
4 Correct 57 ms 9212 KB Output is correct
5 Correct 43 ms 7760 KB Output is correct
6 Correct 73 ms 11712 KB Output is correct
7 Correct 5 ms 3192 KB Output is correct
8 Correct 8 ms 3436 KB Output is correct
9 Correct 14 ms 4088 KB Output is correct
10 Correct 22 ms 4984 KB Output is correct
11 Correct 72 ms 11900 KB Output is correct
12 Correct 10 ms 3496 KB Output is correct
13 Correct 7 ms 3192 KB Output is correct
14 Correct 6 ms 3192 KB Output is correct
15 Correct 8 ms 3448 KB Output is correct
16 Correct 15 ms 3960 KB Output is correct
17 Correct 66 ms 9208 KB Output is correct
18 Correct 6 ms 3192 KB Output is correct
19 Correct 6 ms 3448 KB Output is correct
20 Correct 78 ms 11768 KB Output is correct
21 Correct 6 ms 3320 KB Output is correct
22 Correct 8 ms 3192 KB Output is correct
23 Correct 8 ms 3324 KB Output is correct
24 Correct 6 ms 3192 KB Output is correct
25 Correct 6 ms 3320 KB Output is correct
26 Correct 6 ms 3196 KB Output is correct
27 Correct 6 ms 3320 KB Output is correct
28 Correct 6 ms 3320 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 68 ms 11720 KB Output is correct
2 Correct 108 ms 9592 KB Output is correct
3 Correct 103 ms 9592 KB Output is correct
4 Correct 57 ms 6264 KB Output is correct
5 Correct 28 ms 4984 KB Output is correct
6 Correct 93 ms 10616 KB Output is correct
7 Correct 83 ms 8440 KB Output is correct
8 Correct 79 ms 8312 KB Output is correct
9 Correct 50 ms 6520 KB Output is correct
10 Correct 69 ms 7416 KB Output is correct
11 Correct 116 ms 9724 KB Output is correct
12 Correct 110 ms 10796 KB Output is correct
13 Correct 47 ms 7544 KB Output is correct
14 Correct 79 ms 11640 KB Output is correct
15 Correct 29 ms 4984 KB Output is correct
16 Correct 6 ms 3324 KB Output is correct
17 Correct 5 ms 3192 KB Output is correct
18 Correct 99 ms 8568 KB Output is correct
19 Correct 102 ms 8560 KB Output is correct
20 Correct 54 ms 6008 KB Output is correct
21 Correct 28 ms 4856 KB Output is correct
22 Correct 5 ms 3192 KB Output is correct
23 Correct 36 ms 6136 KB Output is correct
24 Correct 60 ms 6236 KB Output is correct
25 Correct 44 ms 7416 KB Output is correct
26 Correct 56 ms 10488 KB Output is correct
27 Correct 100 ms 11128 KB Output is correct
28 Correct 53 ms 7032 KB Output is correct
29 Correct 51 ms 7032 KB Output is correct
30 Correct 106 ms 9536 KB Output is correct
31 Correct 55 ms 5852 KB Output is correct
32 Correct 47 ms 7388 KB Output is correct
33 Correct 45 ms 7416 KB Output is correct
34 Correct 54 ms 6392 KB Output is correct
35 Correct 48 ms 7416 KB Output is correct
36 Correct 51 ms 8572 KB Output is correct
37 Correct 55 ms 7032 KB Output is correct