确定大小 | ||||||
| ||||||
Description | ||||||
现在有N个字母,(N <= 26),我们称之为A、B、C、D......。这次他们之间想确定大小关系。 但是现在只有一些残缺不全的关系,比如A > Z,然后没了...... 现在给出N个字母,(前N个大写字母),然后给出M个关系,每个关系只有大于和小于两种。 最后判断那些是可以确定他应有的大小位置的。 (没有给出的关系,均属于不可确定) | ||||||
Input | ||||||
多组测试数据:每组测试数据: 第一行两个整数N,M。 接下来M行,每行一个关系,形式如:A>B A<B (M < 30) | ||||||
Output | ||||||
按照ABCD...的顺序,输出可以确定的势力和他的排名。 如果都不可以确定,则输出-1。 | ||||||
Sample Input | ||||||
3 2 A>B B>C 3 3 A>B B>C C>A | ||||||
Sample Output | ||||||
A 1 B 2 C 3 -1 | ||||||
Source | ||||||
2014 Winter Holiday Contest 4 |
图论相关的东西,看了很多AC这个题的代码,基本思路都是一个:bfs、bfs、bfs、这里关系很明确,我们可以用floyd递推关系来确定有多少个人比他厉害,同时当然也能判断有多少个人比他弱、
map【i】【j】==1表示i比j厉害,并且j比i弱,
如果一共有n个人,判断出来有x个人比他弱,有y个人比他强,那么如果x+y==n-1,那么这个人的排名就能够确定了~
那么关系如何确定的呢?floyd递推~
一下子东西就简单了许多~
#include<stdio.h>
#include<string.h>
using namespace std;
int map[27][27];
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
memset(map,0,sizeof(map));
for(int i=0;i<m;i++)
{
char str[10];
scanf("%s",str);
int id1=str[0]-'A';
int id2=str[2]-'A';
if(str[1]=='>')
{
map[id1][id2]=1;
}
else
{
map[id2][id1]=1;
}
}
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
for(int k=0;k<n;k++)
{
if(map[j][i]==1&&map[i][k]==1)
{
map[j][k]=1;
}
}
}
}
int ok=0;
for(int i=0;i<n;i++)
{
int contxiao=0;
int contda=0;
for(int j=0;j<n;j++)
{
if(i!=j)
{
if(map[i][j]==1)
contxiao++;
if(map[j][i]==1)
contda++;
}
}
if(contxiao+contda==n-1)
{
ok=1;
printf("%c %d\n",i+'A',contda+1);
}
}
if(ok==0)
{
printf("-1\n");
}
}
}