sgu 172. eXam dfs二分图判定

本文介绍了一种解决俄罗斯学校学生期末考试安排问题的算法。该算法确保每位学生能在两天内完成所选两门不同科目的考试,且同一天不考重复科目。通过构建图论模型并使用深度优先搜索进行二分图验证,最终确定是否能合理安排考试。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

172. eXam
time limit per test: 0.25 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 

1 4


#include <bits/stdc++.h>
using namespace std;

const int max_d=200+5;
const int max_b=30000*2+5;

bool f=1;
int dis[max_d][max_d];
int head[max_d];
int v[max_d];
int tot=1;
struct node{
    int to;
    int next;
};
node edge[max_b];


void add_edge(int x,int y){
    edge[tot].to=y;
    edge[tot].next=head[x];
    head[x]=tot++;

    edge[tot].to=x;
    edge[tot].next=head[y];
    head[y]=tot++;
}

void dfs(int x,int y){
    v[x]=y;
    for(int i=head[x];i!=0;i=edge[i].next){
        if(v[edge[i].to]==y){
            f=0;
            return;
        }
        else if(v[edge[i].to]==0){
            dfs(edge[i].to,-y);
        }
    }
}

void solve(){
    int a,b;
    scanf("%d%d",&a,&b);

    for(int i=1;i<=b;i++){
        int x,y;
        scanf("%d%d",&x,&y);
        if(dis[x][y]==1)continue;
        dis[x][y]=1;
        dis[y][x]=1;
        add_edge(x,y);
    }

    for(int i=1;i<=a;i++){
        if(v[i]==0){
            dfs(i,1);
            if(f==0){
                printf("no");
                return;
            }
        }
    }

    printf("yes\n");
    int ans=0;
    for(int i=1;i<=a;i++){
        if(v[i]==1)ans++;
    }
    printf("%d\n",ans);
    for(int i=1;i<=a;i++){
        if(v[i]==1){
            printf("%d ",i);
        }
    }

}

int main(){
    solve();
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值