QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#183923 | #5672. Connectivity Problem | MaGnsi0# | RE | 0ms | 3656kb | C++17 | 1.1kb | 2023-09-20 00:58:26 | 2023-09-20 00:58:26 |
Judging History
answer
/**
* author: MaGnsi0
* created: 19.09.2023 19:57:00
**/
#include <bits/stdc++.h>
using namespace std;
struct union_find {
vector<int> parent, length;
union_find(int n) {
parent.resize(n);
iota(parent.begin(), parent.end(), 0);
length = vector<int>(n, 1);
}
int find(int x) {
if (parent[x] != x) {
parent[x] = find(parent[x]);
}
return parent[x];
}
void unite(int x, int y) {
int Px = find(x);
int Py = find(y);
if (Px == Py) {
return;
}
if (length[Px] < length[Py]) {
swap(Px, Py);
}
parent[Py] = Px;
length[Px] += length[Py];
}
};
int main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n;
cin >> n;
union_find dsu(n);
for (int i = 0; i < n; ++i) {
int u; cin >> u; u--;
int v; cin >> v; v--;
cout << (dsu.find(u) == dsu.find(v) ? "Y" : "N") << "\n";
dsu.unite(u, v);
}
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 3656kb
input:
12 3 4 4 9 8 1 2 3 5 6 2 9 5 9 7 3 4 8 5 6 1 8 6 1
output:
N N N N N Y N N N Y Y Y
result:
ok 12 lines
Test #2:
score: -100
Runtime Error
input:
100 26 39 2 21 4 17 2 16 12 19 27 0 8 43 10 12 6 29 5 9 19 32 13 47 13 36 3 6 13 18 9 40 11 40 29 16 7 24 10 35 19 41 6 24 28 21 26 35 23 47 2 30 19 17 10 6 22 6 15 25 19 11 2 8 11 25 14 23 27 1 1 16 16 0 23 34 2 25 10 17 3 35 23 37 13 0 22 7 27 29 15 13 10 5 18 40 28 46 19 0 23 40 4 46 19 3 20 39 1...