結果
| 問題 |
No.1983 [Cherry 4th Tune C] 南の島のマーメイド
|
| コンテスト | |
| ユーザー |
tnakao0123
|
| 提出日時 | 2022-06-20 18:28:12 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 201 ms / 4,000 ms |
| コード長 | 3,654 bytes |
| コンパイル時間 | 928 ms |
| コンパイル使用メモリ | 69,184 KB |
| 実行使用メモリ | 63,080 KB |
| 最終ジャッジ日時 | 2024-10-13 06:26:51 |
| 合計ジャッジ時間 | 10,004 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 41 |
ソースコード
/* -*- coding: utf-8 -*-
*
* 1983.cc: No.1983 [Cherry 4th Tune C] 南の島のマーメイド - yukicoder
*/
#include<cstdio>
#include<vector>
#include<stack>
#include<algorithm>
#include<utility>
using namespace std;
/* constant */
const int MAX_N = 200000;
/* typedef */
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<vi> vvi;
typedef vector<pii> vpii;
typedef stack<int> si;
struct UFT {
vector<int> links, ranks, sizes;
UFT() {}
void init(int n) {
links.resize(n);
for (int i = 0; i < n; i++) links[i] = i;
ranks.assign(n, 1);
sizes.assign(n, 1);
}
int root(int i) {
int i0 = i;
while (links[i0] != i0) i0 = links[i0];
return (links[i] = i0);
}
int rank(int i) { return ranks[root(i)]; }
int size(int i) { return sizes[root(i)]; }
bool same(int i, int j) { return root(i) == root(j); }
int merge(int i0, int i1) {
int r0 = root(i0), r1 = root(i1), mr;
if (r0 == r1) return r0;
if (ranks[r0] == ranks[r1]) {
links[r1] = r0;
sizes[r0] += sizes[r1];
ranks[r0]++;
mr = r0;
}
else if (ranks[r0] > ranks[r1]) {
links[r1] = r0;
sizes[r0] += sizes[r1];
mr = r0;
}
else {
links[r0] = r1;
sizes[r1] += sizes[r0];
mr = r1;
}
return mr;
}
};
/* global variables */
vi nbrs[MAX_N];
UFT uft;
/* subroutines */
void bcc_visit(const vi *nbrs, int v, int u, vpii& brdg, vvi& tecomp,
si& roots, si& S, vb& inS, vi& num, int& time) {
num[v] = ++time;
S.push(v); inS[v] = true;
roots.push(v);
const vi& nbrv = nbrs[v];
for (vi::const_iterator vit = nbrv.begin(); vit != nbrv.end(); vit++) {
const int& w = *vit;
if (num[w] == 0)
bcc_visit(nbrs, w, v, brdg, tecomp, roots, S, inS, num, time);
else if (u != w && inS[w])
while (num[roots.top()] > num[w]) roots.pop();
}
if (v == roots.top()) {
brdg.push_back(pii(u, v));
tecomp.push_back(vi());
for (;;) {
int w = S.top(); S.pop(); inS[w] = false;
tecomp.back().push_back(w);
if (v == w) break;
}
roots.pop();
}
}
void calc_bcc(const int n, const vi *nbrs, vpii& brdg, vvi& tecomp) {
vi num(n);
vb inS(n);
si roots, S;
int time = 0;
for (int u = 0; u < n; u++)
if (num[u] == 0) {
bcc_visit(nbrs, u, n, brdg, tecomp, roots, S, inS, num, time);
brdg.pop_back();
}
}
bool visited[MAX_N];
int depth[MAX_N], low[MAX_N], parents[MAX_N];
int aps[MAX_N], apn;
void getArticulationPoints(int i, int d) {
visited[i] = true;
depth[i] = low[i] = d;
int childCount = 0;
bool isArticulation = false;
vi &nbri = nbrs[i];
for (vi::iterator vit = nbri.begin(); vit != nbri.end(); vit++) {
int &j = *vit;
if (! visited[j]) {
parents[j] = i;
getArticulationPoints(j, d + 1);
childCount++;
if (low[j] >= depth[i]) isArticulation = true;
low[i] = min(low[i], low[j]);
}
else if (j != parents[i])
low[i] = min(low[i], depth[j]);
}
if ((parents[i] >= 0 && isArticulation) ||
(parents[i] < 0 && childCount > 1))
aps[i] = true, apn++;
}
/* main */
int main() {
int n, m, qn;
scanf("%d%d%d", &n, &m, &qn);
for (int i = 0; i < m; i++) {
int a, b;
scanf("%d%d", &a, &b);
a--, b--;
nbrs[a].push_back(b);
nbrs[b].push_back(a);
}
vpii brdgs;
vvi tcmp;
calc_bcc(n, nbrs, brdgs, tcmp);
uft.init(n);
for (auto e: brdgs) uft.merge(e.first, e.second);
while (qn--) {
int x, y;
scanf("%d%d", &x, &y);
x--, y--;
if (uft.same(x, y)) puts("Yes");
else puts("No");
}
return 0;
}
tnakao0123