题意:
判断都多少人无法确定关系
题解:
首先可以将题转换成一个最短路问题,将题目给出 的关系距离为0,没给的距离为0x3f,(单向)走一遍Floyd算法,最后判断。距离是0x3f就将答案+1(因为他俩没有关系),最后输出。
错点
刚开始建立的是单向图,题中也说不会重复询问,最后判断时判断的是上三角,错了6次
最后找了一组数据,
4 2
3 2
4 2
这组数据答案应为4,而我的是6,因为,判断上三角是判断的是
1->2 1->3 1->4
2->3 2->4
3->4
又由于数据中是单向的且是3->2 4->2。这是在上三角中是不会存在的,所以最后判断时
f(a[j][i]==0x3f3f3f3f&&a[i][j]==0x3f3f3f3f)c++;
这样判断才行。
由于数据太大,Floyd需要优化一下
code
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<stack>
#include<set>
#include<map>
#define x first
#define y second
#define met(a,b) memset(a,b,sizeof a)
#define open ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
map<string,int>mapp;
typedef long long ll;
const int N=2500+10;
int n,m,s;
int a[505][505];
void floyd() {
for(int k=1; k<=n; k++) {
for(int i=1; i<=n; i++) {
if(a[i][k]!=0x3f3f3f3f) {//优化
for(int j=1; j<=n; j++) {
a[i][j]=min(a[i][j],a[i][k]+a[k][j]);
}
}
}
}
}
int main() {
int t;
while(cin>>t)
while(t--) {
met(a,0x3f);
scanf("%d%d",&n,&m);
for(int i=1; i<=n; i++) {
a[i][i]=0;
}
while(m--) {
int x,y;
scanf("%d%d",&x,&y);
int xx,yy;
a[x][y]=0;
}
floyd();
int c=0;
for(int i=1; i<=n; i++) {
for(int j=i+1; j<=n; j++) {
if(a[j][i]==0x3f3f3f3f&&a[i][j]==0x3f3f3f3f)c++;
}
}
printf("%d\n",c);
}
}
Problem Description
there are N ACMers in HDU team.
ZJPCPC Sunny Cup 2007 is coming, and lcy want to select some excellent ACMers to attend the contest. There have been M matches since the last few days(No two ACMers will meet each other at two matches, means between two ACMers there will be at most one match). lcy also asks"Who is the winner between A and B?" But sometimes you can’t answer lcy’s query, for example, there are 3 people, named A, B, C.and 1 match was held between A and B, in the match A is the winner, then if lcy asks “Who is the winner between A and B”, of course you can answer “A”, but if lcy ask “Who is the winner between A and C”, you can’t tell him the answer.
As lcy’s assistant, you want to know how many queries at most you can’t tell lcy(ask A B, and ask B A is the same; and lcy won’t ask the same question twice).
Input
The input contains multiple test cases.
The first line has one integer,represent the number of test cases.
Each case first contains two integers N and M(N , M <= 500), N is the number of ACMers in HDU team, and M is the number of matchs have been held.The following M lines, each line means a match and it contains two integers A and B, means A wins the match between A and B.And we define that if A wins B, and B wins C, then A wins C.
Output
For each test case, output a integer which represent the max possible number of queries that you can’t tell lcy.
Sample Input
3
3 3
1 2
1 3
2 3
3 2
1 2
2 3
4 2
1 2
3 4
Sample Output
0
0
4