This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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_];
void init () {
for(int i = 1; i <= N; i++) group[i] = i;
}
int get_group (int v) {
return (group[v] == v) ? v : (group[v] = get_group(group[v]));
}
bool merge (int u, int v) {
u = get_group(u);
v = get_group(v);
if(u == v) return false;
group[u] = v;
return true;
}
};
void dfs (int u, int &l) {
visited[u] = true;
for(int e = 0; e < Gph[u].size(); e++) {
int v = Gph[u][e];
if(l != -1) DS::merge(l, v);
l = v;
if(!visited[v]) dfs(v, l);
}
}
ll res, sz[N_];
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++) {
int t = -1;
if(!visited[i] && Gph[i].size() >= 2) dfs(i, t), visited[i] = false;
}
for(int i = 1; i <= N; i++) sz[DS::get_group(i)]++;
for(int i = 1; i <= N; i++) res += sz[i] * (sz[i]-1);
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;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |