poj 图相关之1094Sorting It All Out
Accepted 144K 0MS
测试数据http://poj.org/showmessage?message_id=133905
我个人觉得这道题有很大的bug,最开始我的代码是这样的,测试数据的第六个显示的是
Inconsistency found after 26 relations.
因为在前25个确实能够排好序,但是第26的时候就出现了Inconsistency(我的check函数就是用作这个的),但是其实这道题其实这要排好序了就用管后面的数据是否Inconsistency了,所以根据题意第6组数据的结果显示为:
Sorted sequence determined after 25 relations: ABCDEFGHIJKLMNOPQRSTUVWXYZ.
ac的版本:
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<math.h>
#include<algorithm>
#include<memory.h>
#include<vector>
#include<queue>
#include<map>
using namespace std;
int n, m, degree[26];
vector<int> vec[26];
vector<int> result;
bool mark[26], vis[26];
int TopSort(int tmp)
{
int i, j, k, count, idx, tmpdegree[26], flag;
result.clear();
memset(vis, 0, sizeof(vis));
for (i = 0; i < 26; i++)
tmpdegree[i] = degree[i];
flag = 1;
for (i = 0; i < tmp; i++)
{
count = 0;
idx = -1;
for (j = 0; j < 26; j++)
{
if (!tmpdegree[j] && mark[j] && !vis[j])
{
count++;
idx = j;
}
}
if (!count)
{
return 0;
}
else
{
if (count > 1)
flag = 2;
result.push_back(idx);
vis[idx] = 1;
for (k = 0; k < vec[idx].size(); k++)
{
tmpdegree[vec[idx][k]]--;
}
}
}
if (flag == 2)
return 2;
else
return 1;
}
int main()
{
//freopen("1.txt", "r", stdin);
int i, j, t, Cases, tm, it;
bool flag;
char tmp[20];
while (scanf("%d%d", &n, &m) != EOF && (n || m))
{
for (i = 0; i < 26; i++)
vec[i].clear();
memset(mark, 0, sizeof(mark));
memset(degree, 0, sizeof(degree));
tm = 0;
t = 2;
for (i = 0; i < m; i++)
{
scanf("%s", tmp);
vec[tmp[0] - 'A'].push_back(tmp[2] - 'A');
if (!mark[tmp[0] - 'A'])
{
mark[tmp[0] - 'A'] = 1;
tm++;
}
if (!mark[tmp[2] - 'A'])
{
mark[tmp[2] - 'A'] = 1;
tm++;
}
degree[tmp[2] - 'A']++;
t = TopSort(tm);
//t为0表示有环 为1表示有唯一排序 为2表示没有唯一排序
if (t == 0)
break;
if (t == 1 && result.size() == n)
{
flag = 1;
for (j = i+1; j < m; j++)
{
scanf("%s", tmp);
}
printf("Sorted sequence determined after %d relations: ", i + 1);
for (j = 0; j < result.size(); j++)
printf("%c", char(result[j] + 'A'));
printf(".\n");
break;
}
}
if (t == 0)
{
for (j = i+1; j < m; j++)
scanf("%s", tmp);
printf("Inconsistency found after %d relations.\n", i + 1);
}
else if (t == 2)
{
printf("Sorted sequence cannot be determined.\n");
}
else if (t == 1 && result.size() != n)
{
printf("Sorted sequence cannot be determined.\n");
}
}
}
个人觉得对的版本:
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<math.h>
#include<algorithm>
#include<memory.h>
#include<vector>
#include<queue>
#include<map>
using namespace std;
int n, m, degree[26];
vector<int> vec[26];
vector<int> result;
bool mark[26], vis[26];
int TopSort(int tmp)
{
int i, j, k, count, idx, tmpdegree[26], flag;
result.clear();
memset(vis, 0, sizeof(vis));
for (i = 0; i < 26; i++)
tmpdegree[i] = degree[i];
flag = 1;
for (i = 0; i < tmp; i++)
{
count = 0;
idx = -1;
for (j = 0; j < 26; j++)
{
if (!tmpdegree[j] && mark[j] && !vis[j])
{
count++;
idx = j;
}
}
if (!count)
{
return 0;
}
else
{
if (count > 1)
flag = 2;
result.push_back(idx);
vis[idx] = 1;
for (k = 0; k < vec[idx].size(); k++)
{
tmpdegree[vec[idx][k]]--;
}
}
}
if (flag == 2)
return 2;
else
return 1;
}
bool check(char*tmp)
{
char a, b;
int i, j, t;
i = j = -1;
for (t = 0; t < result.size(); t++)
{
if (tmp[0] - 'A' == result[t])
i = t;
if (tmp[2] - 'A' == result[t])
j = t;
}
if (i == -1 || j == -1 || i < j)
return true;
return false;
}
int main()
{
//freopen("1.txt", "r", stdin);
int i, j, t, Cases, tm, it;
bool flag;
char tmp[20];
while (scanf("%d%d", &n, &m) != EOF && (n || m))
{
for (i = 0; i < 26; i++)
vec[i].clear();
memset(mark, 0, sizeof(mark));
memset(degree, 0, sizeof(degree));
tm = 0;
t = 2;
for (i = 0; i < m; i++)
{
scanf("%s", tmp);
vec[tmp[0] - 'A'].push_back(tmp[2] - 'A');
if (!mark[tmp[0] - 'A'])
{
mark[tmp[0] - 'A'] = 1;
tm++;
}
if (!mark[tmp[2] - 'A'])
{
mark[tmp[2] - 'A'] = 1;
tm++;
}
degree[tmp[2] - 'A']++;
t = TopSort(tm);
//t为0表示有环 为1表示有唯一排序 为2表示没有唯一排序
if (t == 0)
break;
if (t == 1 && result.size() == n)
{
flag = 1;
for (j = i+1; j < m; j++)
{
scanf("%s", tmp);
if (flag)
{
if (!check(tmp))
{
flag = 0;
it = j;
}
}
}
if (!flag)
{
printf("Inconsistency found after %d relations.\n", it + 1);
}
else
{
printf("Sorted sequence determined after %d relations: ", i + 1);
for (j = 0; j < result.size(); j++)
printf("%c", char(result[j] + 'A'));
printf(".\n");
}
break;
}
}
if (t == 0)
{
for (j = i+1; j < m; j++)
scanf("%s", tmp);
printf("Inconsistency found after %d relations.\n", i + 1);
}
else if (t == 2)
{
printf("Sorted sequence cannot be determined.\n");
}
else if (t == 1 && result.size() != n)
{
printf("Sorted sequence cannot be determined.\n");
}
}
}