目录
D. Pigeon Swap
给每个巢加一个牌子,在第二个操作的时候,对两个巢只要换牌子就可以了。因此每一次询问的时候,都是输出鸽子所在巢的牌子,而不是巢的编号。
任何时候换巢的索引都是牌子。
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e6 + 5, INF = 1e18;
int T, n, q, ans, f[N], h[N], r[N];
string s;
signed main()
{
cin >> n >> q;
for (int i = 1; i <= n; i ++)
f[i] = i, h[i] = i, r[i] = i; // 鸟对应巢 巢对应牌号 牌号对应巢
while (q --)
{
int opt, a, b;
cin >> opt;
if (opt == 1)
{
cin >> a >> b;
f[a] = r[b];
}
if (opt == 2)
{
cin >> a >> b;
swap(h[r[a]], h[r[b]]);
swap(r[a], r[b]);
}
if (opt == 3)
{
cin >> a;
cout << h[f[a]] << '\n';
}
}
return 0;
}
E. Flip Edge
分层图最短路,建图方式是在前 n 个点是第一张图,n 到 2n 个点是第二张图。同时两张图的同一个点要连起来,第二张图的边要反过来。
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 2e5 + 5, INF = 1e18;
struct node
{
int v, w;
};
int T, n, m, x, cnt, ans, d[N * 2], vis[N * 2];
vector<node> G[N * 2];
priority_queue<pair<int, int> > pq;
signed main()
{
cin >> n >> m >> x;
for (int i = 1; i <= m; i ++)
{
int u, v;
cin >> u >> v;
G[u].push_back({v, 1});
G[v + n].push_back({u + n, 1});
}
for (int i = 1; i <= n; i ++)
{
G[i].push_back({i + n, x});
G[i + n].push_back({i, x});
}
for (int i = 1; i <= 2 * n; i ++)
d[i] = INF;
d[1] = 0;
pq.push({0, 1});
while (!pq.empty())
{
auto t = pq.top(); pq.pop();
int u = t.second;
if (vis[u] != 0)
continue;
vis[u] = 1;
for (auto ed : G[u])
{
int v = ed.v, w = ed.w;
if (d[v] > d[u] + w)
{
d[v] = d[u] + w;
pq.push({-d[v], v});
}
}
}
ans = min(d[n], d[2 * n]);
cout << ans;
return 0;
}