제출 #1200598

#제출 시각아이디문제언어결과실행 시간메모리
1200598InvMOD우호 조약 체결 (JOI14_friends)C++17
100 / 100
37 ms6472 KiB
#include<bits/stdc++.h> using namespace std; #define fi first #define se second #define pb push_back #define eb emplace_back #define vi vector<int> #define pi pair<int,int> #define sz(v) (int)(v).size() #define all(v) (v).begin(), (v).end() #define compact(v) (v).erase(unique(all(v)), (v).end()) template<class T> using upq = priority_queue<T, vector<T>, greater<T>>; template<class T> int lwrbound(const vector<T>& a, const T& b, const int s = 0){return int(lower_bound(s + all(a), b) - a.begin());} template<class T> int uprbound(const vector<T>& a, const T& b, const int s = 0){return int(upper_bound(s + all(a), b) - a.begin());} #define FOR(i, a, b) for(int i = (a); i <= (b); i++) #define ROF(i, a, b) for(int i = (a); i >= (b); i--) #define sumof(x) accumulate(all(x), 0ll) #define dbg(x) "[" << #x " = " << (x) << "]" #define el "\n" using ll = long long; using ld = long double; template<class T> bool ckmx(T& a, const T b){return (a < b ? a = b, true : false);} template<class T> bool ckmn(T& a, const T b){return (a > b ? a = b, true : false);} const int N = 2e5 + 5; const int MOD = 1e9 + 7; const ll INF = numeric_limits<ll>::max() / 8; struct DSU{ vector<int> par; DSU(int n = 0): par(n + 1, - 1) {} int asc(int x){ return par[x] < 0 ? x : par[x] = asc(par[x]); } void join(int u, int v){ u = asc(u), v = asc(v); if(u != v){ if(par[u] > par[v]) swap(u, v); par[u] += par[v], par[v] = u; } } bool isPar(int x){ return asc(x) == x; } int gsz(int x){return -par[asc(x)];} }; void Main() { int n,m; cin >> n >> m; vector<vi> adj(n + 1, vi()); FOR(i, 1, m){ int u,v; cin >> u >> v; adj[u].eb(v); } DSU dsu(n); queue<int> q; vector<bool> pushQ(n + 1, false); FOR(i, 1, n)if(sz(adj[i]) > 1){ int pNode = -1; for(int v : adj[i]){ if(pNode == -1) pNode = v; else{ dsu.join(pNode, v); if(!pushQ[v]) q.push(v), pushQ[v] = true; } } if(!pushQ[pNode]) q.push(pNode), pushQ[pNode] = true; } // expand new component while(sz(q)){ int x = q.front(); q.pop(); for(int v : adj[x]){ dsu.join(x, v); if(!pushQ[v]){ q.push(v); pushQ[v] = true; } } } ll ans = 0; FOR(i, 1, n) if(dsu.isPar(i)){ if(dsu.gsz(i) == 1){ ans = ans + sz(adj[i]); //cout << "Node: " << i <<" " << sz(adj[i]) << el; } else{ ll sz_comp = dsu.gsz(i); //cout << "Comp: " << i <<" " << sz_comp << el; ans = ans + 1ll * sz_comp * (sz_comp - 1); } } cout << ans << el; } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define name "InvMOD" if(fopen(name".INP", "r")){ freopen(name".INP", "r", stdin); freopen(name".OUT", "w", stdout); } int t = 1; while(t--) Main(); return 0; } /* a -> b a -> c create b <-> c d -> a d -> c create a <-> c b -> d => (d <-> a, c) d -> b => (d <-> a, c) if a component has size > 1, it can connect to any other component or node that it has an edge to // total edge of a component can be calculate by: size(component) * (size(component) - 1) how about others node connect to a component ? ans: it still need 2 edges to connect to that component so we will create all component and calculate the answer with component that has size = 1 it does not contribute any new edge */

컴파일 시 표준 에러 (stderr) 메시지

friends.cpp: In function 'int32_t main()':
friends.cpp:124:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  124 |         freopen(name".INP", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
friends.cpp:125:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  125 |         freopen(name".OUT", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...