| # | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
|---|---|---|---|---|---|---|---|
| 557985 | 600Mihnea | 조이터에서 친구를 만드는건 재밌어 (JOI20_joitter2) | C++17 | 3 ms | 4948 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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";
}
}
컴파일 시 표준 에러 (stderr) 메시지
| # | Verdict | Execution time | Memory | Grader output |
|---|---|---|---|---|
| Fetching results... | ||||
| # | Verdict | Execution time | Memory | Grader output |
|---|---|---|---|---|
| Fetching results... | ||||
| # | Verdict | Execution time | Memory | Grader output |
|---|---|---|---|---|
| Fetching results... | ||||
