Leaders //dfs

2175: Leaders
Time Limit: 2 Sec  Memory Limit: 256 MB
Submit: 19  Solved: 13
[Submit][Status][Web Board]
Description
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology − to make the leaders work in pairs.


A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple − we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.


Input
The first line contains integers n and m (1≤n≤105,0≤m≤105) − the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1≤a,b≤n). It is guaranteed that the graph has no loops or multiple edges.


Next line contains number q (1≤q≤105) − the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).


Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".


Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7


Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:


1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.


2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.


5) Vertices 1 and 5 are located in different connected components, there's no path between them.



题目意思:输入n,m,n表示点的个数,再输入m行a,b分别表示边界a,b;

再输入q行x,y分别表示要找的点之间的最短路径是奇数还是偶数,奇数输出“Yes”,偶数输出“No”。


心得:第一步是建立边界之间的链接,形成一个图,第二步是进行dfs查找。


#include<bits/stdc++.h>
using namespace std;
const int INF=100010;
vector <int> cc[INF];
int cz[INF];
int x,y,m,n,q,fg;
void dfs(int k,int le)
{
	if(fg) return;  //递归边界
	if(k==y)  //找到结尾。
	{
		if(le%2) fg=1;
		return ;
	}
	for(int i=0;i<cc[k].size();i++)  
	{
		int tp=cc[k][i];
		if(cz[tp])
		{
			cz[tp]=0; //保证走过不重复。
			dfs(tp,le+1);  //递归遍历
			cz[tp]=1;  //走完要复原
		}
	}
}
int main(void)
{
	int a,b;
	scanf("%d%d",&n,&m);
	for(int i=0;i<m;i++)
	{
		scanf("%d%d",&a,&b);
		cc[a].push_back(b);  //将a,b存入数组,相当于建立联系
		cc[b].push_back(a);
	}
	scanf("%d",&q);
	while(q--)
	{
		scanf("%d%d",&x,&y);
		if(x==y)
		{
			printf("No\n");continue; //相等时不对。
		}
		memset(cz,1,sizeof(cz)); //全部设置为未走过状态
		fg=0;
		cz[x]=0;
		dfs(x,0);
		if(fg) printf("Yes\n");
		else printf("No\n");
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值