万妖穴
Case Time Limit: 500 MS (Others) / 1000 MS (Java) Case Memory Limit: 256 MB (Others) / 512 MB (Java)
Accepted: 226 Total Submission: 652
查看我的提交显示标签
Problem Description
“万妖穴中有若干小穴,每处小穴中皆有一只恶妖。小穴外设有封印,汝需消灭指定几处小穴中的恶妖方能解除该小穴封印,进而消灭穴中恶妖。”
“此处石壁所刻便是万妖穴中各穴的封印解除关系。”
“万妖穴中或有部分小穴未设封印,汝可以此作为突破口。”
“吾将根据石壁所示,将各穴消灭顺序给予汝,汝自珍重。”
Input
每个输入文件中一组数据。
第一行两个正整数N、M(1<=N<=1000,0<=M<=N*(N-1)),表示万妖穴中小穴的个数及封印解除关系的条数。
接下来M行,每行两个整数a、b,表示小穴b封印的解除依赖于消灭小穴a中的恶妖。假设N处小穴的编号分别是0~N-1。数据保证a不等于b,且每组封印解除关系a b最多出现一次。
Output
如果能够消灭所有小穴,那么输出一行YES,并在第二行给出用空格隔开的N处小穴的消灭顺序(此处规定,总是消灭当前能消灭的编号最小的小穴);如果不能消灭所有小穴,那么输出一行NO,并在第二行给出无法解除封印的小穴个数。行末均不允许输出多余的空格。
Sample Input 1
3 3
0 1
0 2
2 1
Sample Output 1
YES
0 2 1
Sample Input 2
4 5
0 1
0 2
0 3
2 1
1 2
Sample Output 2
NO
2
Author
Shoutmon
Source
16浙大考研机试模拟赛
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <string>
#include <vector>
#include <queue>
#include <functional>
#include <map>
using namespace std;
const int MaxN = 1010;
const int INF = INT32_MAX;
bool G[MaxN][MaxN];
int N, M;
int degree[MaxN];
bool isVited[MaxN];
int record[MaxN]; int rn = 0;
void Mintree() {
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
if (G[i][j]) ++degree[j];
}
}
priority_queue<int,vector<int>,greater<int>>que;
for (int i = 0; i < N; ++i) {
if (degree[i] == 0)que.push(i);
}
while (que.size()) {
int u = que.top(); que.pop();
record[rn++] = u;
for (int v = 0; v < N; ++v) {
if (G[u][v]) {
if (--degree[v] == 0)que.push(v);
}
}
}
}
int main() {
#ifdef _DEBUG
freopen("data.txt", "r+", stdin);
#endif // _DEBUG
std::ios::sync_with_stdio(false);
cin >> N >> M;
for (int i = 0; i < M; ++i) {
int u, v; cin >> u >> v;
G[u][v] = true;
}
Mintree();
bool flag = true;
int n = 0;
for (int i = 0; i < N; ++i) {
if (degree[i]) {
flag = false;
++n;
}
}
if (flag) {
cout << "YES\n";
for (int i = 0; i < rn; ++i) {
cout << record[i];
if (i != rn - 1)cout << " ";
}
}
else {
cout << "NO\n";
cout << n;
}
return 0;
}