mirror of
https://git.sb/baoshuo/OI-codes.git
synced 2024-11-27 15:16:28 +00:00
parent
6d17789bed
commit
5c67779848
83
S2OJ/1693/1693.cpp
Normal file
83
S2OJ/1693/1693.cpp
Normal file
@ -0,0 +1,83 @@
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
#include <vector>
|
||||
|
||||
using std::cin;
|
||||
using std::cout;
|
||||
const char endl = '\n';
|
||||
|
||||
const int N = 2e5 + 5;
|
||||
|
||||
int n, m, q, c[N << 1], p[N], ans[N << 1], lst[N];
|
||||
bool vis[N];
|
||||
std::vector<int> g[N << 1], circle;
|
||||
|
||||
int dfs1(int u) {
|
||||
if (vis[u]) return u;
|
||||
vis[u] = true;
|
||||
return dfs1(p[u]);
|
||||
}
|
||||
|
||||
void dfs2(int u, int f, int d) {
|
||||
int x = lst[c[u]];
|
||||
ans[u] = std::min({ans[u], ans[f] + 1, d - x + 1});
|
||||
lst[c[u]] = d;
|
||||
for (int v : g[u]) {
|
||||
if (v == f) continue;
|
||||
dfs2(v, u, d + 1);
|
||||
}
|
||||
lst[c[u]] = x;
|
||||
}
|
||||
|
||||
int main() {
|
||||
std::ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
cin >> n >> m;
|
||||
|
||||
for (int i = 1; i <= n; i++) {
|
||||
cin >> c[i];
|
||||
}
|
||||
|
||||
for (int i = 1; i <= n; i++) {
|
||||
cin >> p[i];
|
||||
}
|
||||
|
||||
int root = dfs1(1);
|
||||
std::fill_n(vis, N, false);
|
||||
|
||||
for (int u = p[root]; u != root; u = p[u]) {
|
||||
circle.emplace_back(u);
|
||||
}
|
||||
circle.emplace_back(root);
|
||||
|
||||
for (int x : circle) {
|
||||
vis[x] = true;
|
||||
c[x + n] = c[x];
|
||||
}
|
||||
|
||||
g[p[root] + n].emplace_back(root);
|
||||
for (int i = 1; i < circle.size(); i++) {
|
||||
g[circle[i]].emplace_back(circle[i - 1]);
|
||||
g[circle[i] + n].emplace_back(circle[i - 1] + n);
|
||||
}
|
||||
|
||||
for (int i = 1; i <= n; i++) {
|
||||
if (vis[i] && vis[p[i]]) continue;
|
||||
g[p[i]].emplace_back(i);
|
||||
}
|
||||
|
||||
std::fill_n(ans, N << 1, 0x3f3f3f3f);
|
||||
ans[root + n] = 1;
|
||||
dfs2(root + n, 0, 1);
|
||||
|
||||
cin >> q;
|
||||
|
||||
while (q--) {
|
||||
int x;
|
||||
cin >> x;
|
||||
cout << ans[x] << endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
BIN
S2OJ/1693/data/ex_idiom1.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1693/data/ex_idiom1.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1693/data/ex_idiom1.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1693/data/ex_idiom1.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1693/data/ex_idiom2.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1693/data/ex_idiom2.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1693/data/ex_idiom2.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1693/data/ex_idiom2.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1693/data/ex_idiom3.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1693/data/ex_idiom3.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1693/data/ex_idiom3.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1693/data/ex_idiom3.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1693/data/idiom1.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1693/data/idiom1.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1693/data/idiom1.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1693/data/idiom1.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1693/data/idiom10.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1693/data/idiom10.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1693/data/idiom10.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1693/data/idiom10.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1693/data/idiom11.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1693/data/idiom11.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1693/data/idiom11.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1693/data/idiom11.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1693/data/idiom12.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1693/data/idiom12.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1693/data/idiom12.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1693/data/idiom12.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1693/data/idiom13.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1693/data/idiom13.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1693/data/idiom13.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1693/data/idiom13.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1693/data/idiom14.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1693/data/idiom14.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1693/data/idiom14.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1693/data/idiom14.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1693/data/idiom15.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1693/data/idiom15.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1693/data/idiom15.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1693/data/idiom15.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1693/data/idiom16.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1693/data/idiom16.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1693/data/idiom16.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1693/data/idiom16.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1693/data/idiom17.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1693/data/idiom17.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1693/data/idiom17.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1693/data/idiom17.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1693/data/idiom18.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1693/data/idiom18.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1693/data/idiom18.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1693/data/idiom18.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1693/data/idiom19.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1693/data/idiom19.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1693/data/idiom19.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1693/data/idiom19.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1693/data/idiom2.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1693/data/idiom2.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1693/data/idiom2.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1693/data/idiom2.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1693/data/idiom20.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1693/data/idiom20.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1693/data/idiom20.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1693/data/idiom20.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1693/data/idiom3.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1693/data/idiom3.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1693/data/idiom3.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1693/data/idiom3.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1693/data/idiom4.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1693/data/idiom4.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1693/data/idiom4.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1693/data/idiom4.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1693/data/idiom5.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1693/data/idiom5.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1693/data/idiom5.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1693/data/idiom5.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1693/data/idiom6.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1693/data/idiom6.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1693/data/idiom6.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1693/data/idiom6.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1693/data/idiom7.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1693/data/idiom7.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1693/data/idiom7.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1693/data/idiom7.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1693/data/idiom8.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1693/data/idiom8.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1693/data/idiom8.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1693/data/idiom8.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1693/data/idiom9.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1693/data/idiom9.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1693/data/idiom9.out
(Stored with Git LFS)
Normal file
BIN
S2OJ/1693/data/idiom9.out
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1693/data/problem.conf
(Stored with Git LFS)
Normal file
BIN
S2OJ/1693/data/problem.conf
(Stored with Git LFS)
Normal file
Binary file not shown.
Loading…
Reference in New Issue
Block a user