考虑一个强连通分量,判断其中是否存在权值和非0的环。如果所有环的权值和均为0,可以证明存在给每个点u赋值一个势能\(h_u\)的方案,使得每条边的权值恰好是\((h_v−h_u)\)。通过DFS或者BFS给所有点赋值,检查每条边是否满足条件即可。
#include <bits/stdc++.h>
using namespace std;
vector<int>a[500005],c[500005],w[500005],A[500005];
int dfn[500005],low[500005],tot,cnt,id[500005],d[500005];
long long h[500005];
bool v[500005],f[500005],ins[500005];
stack<int>s;
void tarjan(int n1)
{dfn[n1]=++tot;low[n1]=dfn[n1];s.push(n1);ins[n1]=true;for(int i=0;i<a[n1].size();i++){if(!dfn[a[n1][i]]){tarjan(a[n1][i]);low[n1]=min(low[n1],low[a[n1][i]]);}else if(ins[a[n1][i]]==true) //与无向图不同,有向图的tarjan算法还要加上instack的条件。经典反例:1->0 {low[n1]=min(low[n1],dfn[a[n1][i]]);}}if(dfn[n1]==low[n1]){cnt++;while(s.top()!=n1){id[s.top()]=cnt;w[cnt].push_back(s.top());ins[s.top()]=false;s.pop();}id[n1]=cnt;w[cnt].push_back(n1);s.pop();ins[n1]=false;}
}
queue<int>q;
void topsort()
{for(int i=1;i<=cnt;i++){if(d[i]==0){q.push(i);}}while(q.size()){int n1=q.front();q.pop();for(int i=0;i<A[n1].size();i++){d[A[n1][i]]--;if(d[A[n1][i]]==0){q.push(A[n1][i]);}f[A[n1][i]]|=f[n1];}}
}
int main()
{ios::sync_with_stdio(false);cin.tie(0);int n,m,Q;cin>>n>>m>>Q;for(int i=1;i<=m;i++){int u,v;cin>>u>>v;int w=v;v=u+v;u=(u%n+n)%n;v=(v%n+n)%n;a[u].push_back(v);c[u].push_back(w);}for(int i=0;i<n;i++){if(!dfn[i]){tarjan(i);}}for(int i=1;i<=cnt;i++){q.push(w[i][0]);h[w[i][0]]=0;v[w[i][0]]=true;while(q.size()){int n1=q.front();q.pop();for(int j=0;j<a[n1].size();j++){if(id[a[n1][j]]==id[n1]){if(v[a[n1][j]]==false){v[a[n1][j]]=true;h[a[n1][j]]=h[n1]+c[n1][j];q.push(a[n1][j]);}else{if(h[n1]+c[n1][j]!=h[a[n1][j]]){f[i]=true;}}}}}}for(int i=0;i<n;i++){for(int j=0;j<a[i].size();j++){if(id[i]!=id[a[i][j]]){A[id[a[i][j]]].push_back(id[i]);d[id[i]]++;}}}topsort();for(int i=1;i<=Q;i++){int x;cin>>x;x=(x%n+n)%n;f[id[x]]==true? cout<<"Yes\n":cout<<"No\n";}return 0;
}
/*
9 12 0
0 1
1 2
2 0
2 3
3 4
4 3
1 5
5 6
6 7
7 8
8 5
8 7
*/