제출 #42366

#제출 시각아이디문제언어결과실행 시간메모리
42366nickyrio우호 조약 체결 (JOI14_friends)C++14
0 / 100
100 ms27188 KiB
#include <bits/stdc++.h> #define FOR(i, a, b) for (int i = a; i<= b; ++i) #define FORD(i, a, b) for (int i = a; i>=b; --i) #define REP(i, a) for (int i = 0; i<a; ++i) #define DEBUG(x) { cerr << #x << " = " << x << endl; } #define Arr(A, l, r) { cerr << #A << " = "; FOR(_, l, r) cerr << A[_] << ' '; cerr << endl; } #define N 1001000 #define pp pair<int, int> #define next __next #define prev __prev #define __builtin_popcount __builtin_popcountll #define bit(S, i) (((S) >> i) & 1) #define IO ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL); using namespace std; int n, m, f[N]; vector<int> e[N]; void Union(int a, int b) { int t = f[a] + f[b]; if (f[a] > f[b]) swap(a, b); f[a] = t; f[b] = a; } int root(int u) { return f[u] < 0 ? u : (f[u] = root(f[u])); } int main() { IO; cin >> n >> m; REP(i, m) { int u, v; cin >> u >> v; e[u].push_back(v); } FOR(i, 1, n) f[i] = -1; FOR(u, 1, n) { int r = -1; for (int v : e[u]) { if (r == -1) r = v; else { int a = root(r); int b = root(v); if (a != b) Union(a, b); } } } long long ans = 0; FOR(u, 1, n) { for (int v : e[u]) if (root(u) != root(v)) ans++; if (f[u] < 0) ans += 1ll * f[u] * (f[u] + 1); } cout << ans; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...