Submission #43259

#TimeUsernameProblemLanguageResultExecution timeMemory
43259smu201111192우호 조약 체결 (JOI14_friends)C++14
0 / 100
55 ms13088 KiB
#include <iostream> #include <cstdio> #include <cassert> #include <bitset> #include <string> #include <sstream> #include <algorithm> #include <set> #include <numeric> #include <cmath> #include <map> #include <vector> #include <queue> #include <stack> #include <cstring> #include <queue> #include <numeric> #include <iomanip> #define ll long long using namespace std; const int MAXN = 400001; int n,m,vis[MAXN],parent[MAXN],sz[MAXN]; vector<int> adj[MAXN]; int find(int u) { return u == parent[u] ? u : parent[u] = find(parent[u]); } void mrg(int u,int v){ u = find(u) , v = find(v); if(u == v)return; parent[u] = v; sz[v] += sz[u]; } void dfs(int here,int root){ vis[here] = 1; for(auto x:adj[here]){ if(vis[x]) mrg(root,x); else{ mrg(root,x); dfs(x,root); } } } int main(){ ios::sync_with_stdio(false); cin.tie(0); cin >> n >> m; for(int i = 1; i <= n; i++) parent[i] = i, sz[i] = 1; for(int i = 1; i <= m; i++){ int u,v; cin >> u >> v; adj[u].push_back(v); } for(int i = 1; i <= n; i++){ if(adj[i].size() >= 2){ dfs(i,adj[i].front()); } } long long ans = 0; for(int i = 1; i <= n; i++)if(i == find(i)) ans += 1LL * sz[i] * (sz[i] - 1); for(int i = 1; i <= n; i++){ for(auto x:adj[i]){ if(find(i) != find(x)) ans++; else ans--; } } cout<<ans; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...