# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
557985 | 2022-05-06T12:28:55 Z | 600Mihnea | 조이터에서 친구를 만드는건 재밌어 (JOI20_joitter2) | C++17 | 3 ms | 4948 KB |
#include <bits/stdc++.h> bool home = 1; using namespace std; typedef long long ll; const int N=(int)1e5+7; int n; int m; int t[N]; int sz[N]; ll sol=0; set<int> g[N]; int root(int a) { if (t[a]==a) return a; return t[a]=root(t[a]); } ll contrib(int i) { i=root(i); return ((ll)g[i].size()-1)*(ll)sz[i]; } void join(int a,int b){ a=root(a); b=root(b); if(a==b) { return; } if ((int)g[a].size()>(int)g[b].size()) swap(a, b); sol-=contrib(a); sol-=contrib(b); for (auto &j:g[a]) { g[b].insert(j); } t[a]=b; g[a].clear(); sz[b]+=sz[a]; sol+=contrib(b); } signed main() { #ifdef ONLINE_JUDGE home = 0; #endif home=0; if (home) { freopen("I_am_iron_man", "r", stdin); } else { ios::sync_with_stdio(0); cin.tie(0); } cin>>n>>m; for (int i=1;i<=n;i++) { t[i]=i; sz[i]=1; g[i]={i}; } vector<pair<int, int>> edges; for (int i=1;i<=m;i++) { int a,b; cin>>a>>b; sol-=contrib(b); g[root(b)].insert(a); sol+=contrib(b); edges.push_back({a, b}); bool is=0; for (auto &it:edges){ if(root(it.first)==root(b)&&root(it.second)==root(a)) { is=1; } } if (is) { join(a, b); } cout<<sol<<"\n"; } }
Compilation message
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 3 ms | 4948 KB | Output is correct |
2 | Correct | 3 ms | 4948 KB | Output is correct |
3 | Incorrect | 3 ms | 4948 KB | Output isn't correct |
4 | Halted | 0 ms | 0 KB | - |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 3 ms | 4948 KB | Output is correct |
2 | Correct | 3 ms | 4948 KB | Output is correct |
3 | Incorrect | 3 ms | 4948 KB | Output isn't correct |
4 | Halted | 0 ms | 0 KB | - |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 3 ms | 4948 KB | Output is correct |
2 | Correct | 3 ms | 4948 KB | Output is correct |
3 | Incorrect | 3 ms | 4948 KB | Output isn't correct |
4 | Halted | 0 ms | 0 KB | - |