码迷,mamicode.com
首页 > 其他好文 > 详细

1134. Vertex Cover (25)

时间:2018-03-14 20:07:12      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:test   pos   ice   有关   sizeof   ecif   ++   c++   main   

vertex cover of a graph is a set of vertices such that each edge of the graph is incident to at least one vertex of the set. Now given a graph with several vertex sets, you are supposed to tell if each of them is a vertex cover or not.

Input Specification:

Each input file contains one test case. For each case, the first line gives two positive integers N and M (both no more than 104), being the total numbers of vertices and the edges, respectively. Then M lines follow, each describes an edge by giving the indices (from 0 to N-1) of the two ends of the edge.

After the graph, a positive integer K (<= 100) is given, which is the number of queries. Then K lines of queries follow, each in the format:

Nv v[1] v[2] ... v[Nv]

where Nv is the number of vertices in the set, and v[i]‘s are the indices of the vertices.

Output Specification:

For each query, print in a line "Yes" if the set is a vertex cover, or "No" if not.

Sample Input:

10 11
8 7
6 8
4 5
8 4
8 1
1 2
1 4
9 8
9 1
1 0
2 4
5
4 0 3 8 4
6 6 1 7 5 4 9
3 1 8 4
2 2 8
7 9 8 7 6 5 4 2

Sample Output:

No
Yes
Yes
No
No

 邻接表,每次记录与某个点有关的边的条数,然后标记这个点。最后判断是否是能有m条边就ok。

代码:

技术分享图片
#include <bits/stdc++.h>
using namespace std;
int first[80000],nex[80000],u[80000],v[80000];
int n,m,k,nv,ve,visited[40001];
int main()
{
    memset(first,-1,sizeof(first));
    cin>>n>>m;
    for(int i = 0;i < m;i ++)
    {
        cin>>u[i]>>v[i];
        nex[i] = first[u[i]];
        first[u[i]] = i;
        u[i + m] = v[i];
        v[i + m] = u[i];
        nex[i + m] = first[u[i + m]];
        first[u[i + m]] = i + m;
    }
    cin>>k;
    while(k --)
    {
        int c = 0;
        cin>>nv;
        for(int i = 0;i < nv;i ++)
        {
            cin>>ve;
            visited[ve] = k;
            int ver = first[ve];
            while(ver != -1)
            {
                if(visited[v[ver]] != k)
                {
                    c ++;
                }
                ver = nex[ver];
            }
        }
        if(c == m)cout<<"Yes"<<endl;
        else cout<<"No"<<endl;
    }
}
View Code

 

1134. Vertex Cover (25)

标签:test   pos   ice   有关   sizeof   ecif   ++   c++   main   

原文地址:https://www.cnblogs.com/8023spz/p/8569674.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!