이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "game.h"
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<ll>;
struct dsu {
vi r, p;
unordered_map<int, int> cnt[1500];
void init(int n) {
r.resize(n+1);
p.resize(n+1);
for(int i = 0; i <= n; i++) p[i] = i;
}
int par(int i) {
if(i == p[i]) return i;
return p[i] = par(p[i]);
}
void unite(int u, int v) {
u=par(u),v=par(v);
if(u==v) return;
if(r[u]<r[v]) swap(u, v);
p[v] = u;
r[u] += r[u]==r[v];
for(auto i : cnt[v])
cnt[u][i.first] += i.second;
cnt[v].clear();
}
};
#define left afsjlk
dsu x;
set<pair<int, int>> left;
int n;
void initialize(int N) {
n=N;
x.init(n);
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
x.cnt[i][j]++;
}
void upd() {
for(int i = 0; i < n; i++) {
if(x.cnt[i].empty()) continue;
unordered_map<int, int> t;
for(auto j : x.cnt[i])
t[x.par(j.first)]+=j.second;
swap(t, x.cnt[i]);
}
}
int hasEdge(int u, int v) {
if(u>v)swap(u, v);
if((u=x.par(u))==(v=x.par(v)))
return 1;
//cout << x.cnt[u][v] << '\n';
int t = x.cnt[u][v];x.cnt[u][v]--, x.cnt[v][u]--;
if(t==1) x.unite(u, v), upd();
return t==1;
}
/*
int main() {
cin.tie(0);
initialize(4);
for(int f, t, i = 0; i < 6; i++) {
cin >> f >> t;
cout << hasEdge(f, t) << "\n";
}
}
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |