1.Edge Game
容易想到的一个思路是:找到两点间的最短路,然后判断最短路的奇偶性。奇数则赢,偶数则负。
最短路可以跑D i j s t r a DijstraDijstra,也可以S P F A SPFASPFA,但是需要堆优化(p r i o r i t y _ q u e u e priority_queuepriority_queue),直接C o p y CopyCopy标准模板修改一下即可。
需要注意的是:1.建双向边 2.边权置1
#include <bits/stdc++.h>
#define itn int
#define ll long long
#define rnt register int
#define ull unsigned long long
#define IOF ios_base::sync_with_stdio(false)
using namespace std;
const int N = 1e5 + 10;
const int INF = 0x3f3f3f3f;
int dis[N];
bool via[N];
int n, m, a, b;
struct node {
int to, dis;
node(int tt = 0, int dd = 0) : to(tt), dis(dd) {}
friend bool operator<(node n1, node n2){ return n1.dis > n2.dis; }
};
vector<node> vec[N];
priority_queue<struct node> q;
int dijstra(int start, int end){
memset(via, false, sizeof(via));
dis[start] = 0;
q.push(node(start, 0));
while (!q.empty()){
node now = q.top();
q.pop();
if (!via[now.to]){
via[now.to] = true;
for (int i = 0; i < vec[now.to].size(); i++){
int to = vec[now.to][i].to;
int cost = vec[now.to][i].dis + dis[now.to];
if (cost < dis[to]){
dis[to] = cost;
q.push(node(to, cost));
}
}
}
}
return dis[end];
}
signed main(){
IOF; cin >> n;
for (int i = 1; i <= n; i++){
dis[i] = INF;
vec[i].clear();
}
for (int i = 1; i <= n - 1; i++){
int u, v; cin >> u >> v;
vec[u].push_back(node(v, 1));
vec[v].push_back(node(u,1));
}
cin >> a >> b;
if(dijstra(a, b) % 2 == 0) cout << "No" << endl;
else cout << "Yes" << endl;
return 0;
}
Group QQ Speed
如果所有人在同一组里,由于每个人都可能会禁掉不同的地图,因此我们应该有n + 1 n + 1n+1张地图;
如果每个人单独一组,则需要2张图;
其余情况下,只需要3张地图即可满足题意
#include <bits/stdc++.h>
#define itn int
#define ll long long
#define rnt register int
#define ull unsigned long long
#define IOF ios_base::sync_with_stdio(false)
#pragma GCC optimize("no-stack-protector")
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
signed main(){
IOF;
int T = 0; cin >> T;
while (T--){
int n, m; cin >> n >> m;
if (n == m) cout << 2 << endl;
else if (m == 1) cout << n + 1 << endl;
else cout << 3 << endl;
}
}