Programming thinking week7 homework A-game wins and losses table

topic

Magic Cat tells TT that it actually has a game win-loss table with N individuals and M win-loss relationships. Each win-loss relationship is AB, indicating that A can win B, and the win-loss relationship is transitive. That is, A beats B, and B beats C, so A can beat C.
TT does n’t believe that his kitten can predict any game, so he wants to know how many players ’outcomes cannot be known in advance. Can you help him?

Input

The first line gives the number of data groups.
The first line of each set of data gives N and M (N, M <= 500).
In the next M lines, each line is given AB, indicating that A can beat B.

Ouput

For each set of data, it is impossible to know in advance how many games are won or lost. Note that (a, b) is equivalent to (b, a), that is, each binary is calculated only once.

Sample Input

3
3 3
1 2
1 3
2 3
3 2
1 2
2 3
4 2
1 2
3 4

Sample Ouput

0
0
4

Ideas

Considering each player as a point, if player A can beat player B, there is a directed edge from point A to point B. Then the problem is abstracted as whether any two points of the directed graph are connected .
Using floyd algorithm, change the distance between two points to find whether there is a directional connection between the two points.
The changed code is:

void floyd(int& ans){
    for(int k=1;k<=n;k++){
        for(int i=1;i<=n;i++){
            if(dis[i][k]==1){
                for(int j=1;j<=n;j++){
                    if(dis[k][j]==1&&dis[i][j]!=1){
                        dis[i][j]=1;
                        ans++;
                    }
                }
            }
        }
    }
}

Note thatPruningIfdis [i] [k]! = 1, Meaning wearThere is no directed edge where i points to k, Not to mention that i-> j is derived from i-> k and k-> j.

Floyd-Warshall

  • official:f[x][y]=min(f[x][y],f[x][k]+f[k][y]). The distance from x to y is updated to be the smaller of the original distance from x to y and the distance from x to y when passing through nodes 1 to k. That is, the distance between the two points corresponding to the initialization edge is removed, and the distance between the remaining two points is assigned as + ∞. Continuously update the distance between any two points. When k = n, the update ends.
  • Algorithm implementation (notek is the outermost cycleInsert picture description here
  • Application: used to find the figureThe relationship between any two points: Multi-source shortest, anyDistance relationship between two points; Transitive closures on the graph, arbitraryConnectivity of two points

Code

#include <cstdio>
#include <algorithm>
using namespace std;
int dis[505][505];
int n,m;

void floyd(int& ans){
    for(int k=1;k<=n;k++){
        for(int i=1;i<=n;i++){
            if(dis[i][k]==1){
                for(int j=1;j<=n;j++){
                    if(dis[k][j]==1&&dis[i][j]!=1){
                        dis[i][j]=1;
                        ans++;
                    }
                }
            }
        }
    }
}

int main(){
    int num;
    scanf("%d",&num);
    for(int i=0;i<num;i++){
        memset(dis,0,sizeof(dis));
        scanf("%d%d",&n,&m);
        for(int j=0;j<m;j++){
            int a,b;
            scanf("%d%d",&a,&b);
            dis[a][b]=1;
        }
        int ans=m;
        floyd(ans);
        printf("%d\n",n*(n-1)/2-ans);
    }
    return 0;
}

Title link

Published 24 original articles · praised 2 · visits 435

Guess you like

Origin blog.csdn.net/weixin_43805228/article/details/105398531