sgu 172 eXam

10 篇文章 0 订阅
1 篇文章 0 订阅
172. eXam
time limit per test: 0.5 sec.
memory limit per test: 4096 KB
input: standard
output: standard



In Russia school pupils must do some exams before leaving school. Among others, they must do two "selective" exams. This means that school provides a list of available subjects; each pupil selects two different subjects from this list and is going to do this exams. According to rules, pupil isn't allowed to do both exams at the same day, so the school must schedule the exams, i.e. provide some days when pupils will be able to do exams.

One school does not want to warn teachers too much. They want to schedule all the exams into two days in such way that exams on some subjects are on the first day, and exams on all other (and only on them) are on second. You are to write a program, which will determine, if it is possible to schedule exams in this way so that all pupils will be able to do all their selected exams.

Input
On the first line of input there are two integers N and M (1<=N<=200, 1<=M<=30000) - the number of available subjects and the number of pupils. Then M lines follows; on i-th of them there are two integers - the numbers of exams, which were selected by i-th pupil. Exams are numerated from 1 to N.

Output
If the solution exists, write on the first line of output only one word "yes". On the second line write the total number of exams, which must be held on first day, and on the third line - the numbers of subjects of this exams. If there exist several solutions, output any. If no solution exists, write to output only one word "no".

Sample test(s)

Input
4 4
1 2
3 4
2 4
1 3

Output
yes
2
1 4

本题有很多种解法,比较常见的有用并查集实现。
这里我用的是类似于2—sat的方法,将每个点拆成两个+i -i 分别表示选和不选,这里选和不选都是指第一天的,那么我们知道若某个人的两门课是 (i,j) 在+i和-j连边,在-i和+j连边。然后做一遍dfs就可以算出是否可行。

我的代码:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<set>
#include<algorithm>
#include<vector>
#include<cstdlib>

#define inf 0xfffffff
#define CLR(a,b) memset((a),(b),sizeof((a)))
#define FOR(a,b) for(int a=1;a<=(b);(a)++)

using namespace std;
int const nMax = 1010;
int const base = 10;
typedef int LL;
typedef pair<LL,LL> pij;

//    std::ios::sync_with_stdio(false);

vector<pij> adj[nMax][2];
int n,m;

int vis[nMax][2];
void insert(int u,int p,int v,int q){
    adj[u][p].push_back(pij(v,q));
    return ;
}

int sum;

bool dfs(int u,int p){
    if(p==0) sum++;
    vis[u][p]=1;
    pij f;
    int v,q;
    for(int i=0;i<adj[u][p].size();i++){
        f=adj[u][p][i];
        v=f.first,q=f.second;
        if(vis[v][q])continue;
        if(vis[v][1-q])return false;
        if(!dfs(v,q)) return false;
    }
    return true;
}

int main(){
        scanf("%d%d",&n,&m);
        FOR(i,n) FOR(j,2) adj[i][j-1].clear();
        CLR(vis,0);
        int u,v;
        FOR(i,m) {
            scanf("%d%d",&u,&v);
            insert(u,0,v,1);
            insert(u,1,v,0);
            insert(v,0,u,1);
            insert(v,1,u,0);
        }
        sum=0;
        FOR(i,n) if(!vis[i][0]&&!vis[i][1])
          if(!dfs(i,0)) {
                puts("no");
                return 0;
          }
        puts("yes");
        printf("%d\n",sum);
        FOR(i,n) if(vis[i][0]) printf("%d ",i);
        printf("\n");
        return 0;
}



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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值