HDU - 1704 - Rank (floyd transitive closure)

[Link title] ( https://vjudge.net/contest/364766#problem/C)
subject to the effect: only one between every two points represent two sides of the relationship, the relationship is transitive, asked how many points between It does not matter.
  This use of the transitive closure problem to solve. We can get the title in accordance with an input \ (a-> b \) edge, indicates \ (A \) Win \ (B \) , then we can turn to establish a \ (a <-b \) side, representation \ (b \) lost \ (a \) , then in the case of a total of 4.
  1. If \ (A-> B-> C \) , it can be derived \ (A \) Win \ (C \) .
  2. If \ (A <-b <-C \) , it can be derived \ (A \) lost \ (C \) .
  3. If \ (a-> b <-c \ ) , we can not Release \ (A \) and \ (C \) relationship.
  4. If \ (A <-b-> C \) , we can not be introduced\ (a \) and \ (c \) relations.
  From the several cases above we can see that the only relationship in the same transfer direction, we can get the relationship between two paths through this path, not vice versa. Therefore, we expressed. 1 \ (A \) Win \ (B \) , represented by -1 \ (A \) lost \ (B \) , to seek floyd by
two points are.

int n, m, g[maxn][maxn];
void init() {
    for (int i = 1; i<=n; ++i)
        for (int j = 1; j<=n; ++j)
            g[i][j] = 0;
}
void floyd() {
    for (int k = 1; k<=n; ++k)
        for (int i = 1; i<=n; ++i)
            if (g[i][k])
                for (int j = 1; j<=n; ++j)
                    if (!g[i][j] && g[i][k] && g[k][j] && g[i][k]==g[k][j])
                        g[i][j] = g[i][k];
}
int checker() {
    int ans = 0;
    for (int i = 1; i<=n; ++i)
        for (int j = i+1; j<=n; ++j)
            if (!g[i][j]) ++ans;
    return ans;
}
int main(void) {
    int t;
    scanf("%d", &t);
    while(t--) {
        scanf("%d%d", &n, &m);
        init();
        for (int i = 0, a, b; i<m; ++i) {
            scanf("%d%d", &a, &b);
            g[a][b] = 1;
            g[b][a] = -1;
        }
        floyd();
        printf("%d\n", checker());
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/shuitiangong/p/12586564.html