http://acm.hdu.edu.cn/showproblem.php?pid=4696
由题意可知 1<=Ci<=2 而且图上一定有环
那么我们可以得出:
只要存在奇环(即Ci=1)..那么对于所有自然数Mi都能得到
否则就是偶环 只有Mi是偶数才能得到
/********************* Template ************************/ #include <set> #include <map> #include <list> #include <cmath> #include <ctime> #include <deque> #include <queue> #include <stack> #include <bitset> #include <cstdio> #include <string> #include <vector> #include <cassert> #include <cstdlib> #include <cstring> #include <sstream> #include <fstream> #include <numeric> #include <iomanip> #include <iostream> #include <algorithm> #include <functional> using namespace std; #define EPS 1e-8 #define MAXN 100005 #define MOD (int)1e9+7 #define PI acos(-1.0) #define DINF (1e10) #define LINF ((1LL)<<50) #define INF (0x3f3f3f3f) #define max(a,b) ((a) > (b) ? (a) : (b)) #define min(a,b) ((a) < (b) ? (a) : (b)) #define max3(a,b,c) (max(max(a,b),c)) #define min3(a,b,c) (min(min(a,b),c)) #define BUG cout<<"BUG! "<<endl #define line cout<<"--------------"<<endl #define L(t) (t << 1) #define R(t) (t << 1 | 1) #define Mid(a,b) ((a + b) >> 1) #define lowbit(a) (a & -a) #define FIN freopen("in.txt","r",stdin) #define FOUT freopen("out.txt","w",stdout) #pragma comment (linker,"/STACK:102400000,102400000") // typedef long long LL; // typedef unsigned long long ULL; // typedef __int64 LL; // typedef unisigned __int64 ULL; // LL gcd(LL a,LL b){ return b?gcd(b,a%b):a; } // LL lcm(LL a,LL b){ return a*b/gcd(a,b); } /********************* F ************************/ int to[MAXN]; bool vis[MAXN]; int val[MAXN]; int main() { //FIN; //FOUT; int n,Q,c; while(~scanf("%d%d",&n,&Q)){ memset(vis,false,sizeof(vis)); for(int i = 1 ; i <= n ; i++) scanf("%d",&to[i]); for(int i = 1 ; i <= n ; i++) scanf("%d",&val[i]); bool odd_cir = false; for(int i = 1 ; i <= n && !vis[i] ; i++){ if(val[i] == 1){ int res = 0; for(int j = i ;; j = to[j]){ res += val[i]; vis[j] = true; if(vis[to[j]] == true) { odd_cir = true; break; } } if(odd_cir == true) break; } } for(int i = 0 ; i < Q ; i++){ scanf("%d",&c); if(c <= 0) puts("NO"); else { if(odd_cir == true) puts("YES"); else{ if(c % 2 == 0) puts("YES"); else puts("NO"); } } } } return 0; }