http://acm.hdu.edu.cn/showproblem.php?pid=3357
本题的意思就是按时间顺序构建一个图,如果在当前状态下再加一条边出现了环路就不加这条边,并记录加一,如果没有出现环路就把这条边加进去。
数据中有很多重复的数据,所以处理时,重复的数据就不用考虑了,不然很浪费时间。
#include <stdio.h>
#include <string.h>
#define MAX 238
#define bool int
#define true 1
#define false 0
bool map[MAX][MAX];
int n, t;
void update(int a, int b)
{
int i, j;
map[a][b] = true;
for(i = 1; i <= n; i ++){
if(map[i][a]){
map[i][b] = true;
}
}
for(i = 1; i <= n; i ++){
if(map[b][i]){
map[a][i] = true;
for(j = 1; j <= n; j ++){
if(map[j][b]){
map[j][i] = true;
}/*if*/
}/*for*/
}/*if*/
}/*for*/
}
int main()
{
int a, b, i;
int cas = 0, res;
while(scanf("%d %d", &n, &t)){
if(n == 0 && t == 0)break;
res = 0;
memset(map, false, sizeof(map));
for(i = 0; i < t; i ++){
scanf("%d %d", &a, &b);
if(a == b)res ++;
else if(map[b][a])res ++;
else if(map[a][b] == 0)//有很多重复的数据,这里会忽略重复的数据
update(a, b);
}
printf("%d. %d\n", ++cas, res);
}
return 0;
}