#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <memory.h>
#include <math.h>
#include <assert.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
#include <string>
#include <functional>
#include <vector>
#include <deque>
#include <utility>
#include <bitset>
#include <limits.h>
using namespace std;
typedef long long ll;
typedef unsigned long long llu;
typedef double lf;
typedef unsigned int uint;
typedef long double llf;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
#define pb push_back
const int N_ = 100005;
int N, M;
vector<int> Gph[N_];
bool visited[N_];
namespace DS {
int group[N_], sz[N_];
void init () {
for(int i = 1; i <= N; i++) group[i] = i, sz[i] = 1;
}
int get_group (int v) {
return (group[v] == v) ? v : (group[v] = get_group(group[v]));
}
int merge (int u, int v) {
u = get_group(u);
v = get_group(v);
if(u == v) return false;
sz[v] += sz[u]; sz[u] = 0;
group[u] = v;
return true;
}
};
void dfs (int u) {
visited[u] = true;
for(int e = 0; e < Gph[u].size(); e++) {
int v = Gph[u][e];
DS::merge(u, v);
if(!visited[v]) dfs(v);
}
}
ll res;
int main() {
scanf("%d%d", &N, &M);
for(int i = 1; i <= M; i++) {
int u, v; scanf("%d%d", &u, &v);
Gph[u].pb(v);
}
DS::init();
for(int i = 1; i <= N; i++) {
if(!visited[i] && Gph[i].size() >= 2) dfs(i);
}
for(int i = 1; i <= N; i++) res += (ll)DS::sz[i] * (DS::sz[i] - 1) / 2;
for(int i = 1; i <= N; i++) {
for(int e = 0; e < Gph[i].size(); e++) {
int j = Gph[i][e];
int a = DS::get_group(i), b = DS::get_group(j);
if(a != b) ++res;
}
}
printf("%lld\n", res);
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
4428 KB |
Output is correct |
2 |
Correct |
0 ms |
4428 KB |
Output is correct |
3 |
Incorrect |
0 ms |
4428 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
4428 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
60 ms |
5484 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |