HDU-5961 Transmission (violence)

Source: Internet
Author: User
Tags bitset printf first row


Pass Time limit:12000/6000 MS (java/others) Memory limit:65536/65536 K (java/others)
Total submission (s): 1140 Accepted Submission (s): 514


Problem Description We call a graph G isPass theIf and only if there is an edge from A to B in any of the three different vertex A and there is an edge from B to C, then there is also an edge in G from A to C.
We say that figure G is aCompetition ChartWhen and only if it is a graph and its jhoira is a complete graph. In other words, directing the full graph to each edge will get a race chart.
The following illustration shows a race chart with 4 vertices.

Now, give you two of the graph P = (V, Ep) and q = (V, Ee), satisfying:
1. EP and EE do not have a common edge;
2. (V, Ep⋃ee) is a contest chart.
Your task is to determine whether P,q is passed at the same time.


Input contains up to 20 sets of test data.
The first line has a positive integer that represents the number of groups of data.
For each set of data, the first row has a positive integer n. The next n lines, each of the consecutive n characters, each character may only be one of '-', ' P ', ' Q '.
∙ If the J character of Line I is ' P ', it indicates that there is an edge from I to J in the direction graph P;
∙ If the J character of Line I is ' Q ', it indicates that there is an edge from I to J in the direction graph Q;
∙ Otherwise there is no edge from I to J in two figures.
Guaranteed 1 <= N <= 2016, and not more than 16000 of the number of sets of data in a test point. Ensure that the input graph must satisfy the given constraints.

Output for each data, you need to export one line. If P! Q are all passed, so please output ' T '. Otherwise, output ' N ' (all quotation marks are not included).
Sample Input
4 4-PPP--PQ---q----4-p-p--pq p--q----4-PPP--qq------Q-4-ppp--PQ------Q
Sample Output
T n T n Hint in the diagram below, the left image is Q.Note: in Example 2, p is not passed. In Sample 4, Q is not passed.
The puzzle: Violence

If you are the parent of V, V is the parent node of W, then you must also be the parent of W, using Bitset to record the parent node of each point, run through SPFA, and finally enumerate the parent node of each point to see if a side is attached to the parent node


#include <bits/stdc++.h> using namespace std;
const int MX = 2020;
Char MP[MX][MX];
bitset<mx>b[mx]; struct Edge {int V, NXT;}
E[MX*MX], edge[mx*mx];
int HEAD[MX], head[mx], tot, rear, n;
int P[MX], p1[mx], p2[mx], vis[mx];
    void Init () {memset (head,-1, sizeof (head));
    Memset (Head,-1, sizeof (head));
    for (int i = 0; i < n; i++) p1[i] = p2[i] = i;
Tot = rear = 0;
    } void ADD (int u, int v) {e[tot].v = v;
    E[TOT].NXT = Head[u];
Head[u] = tot++;
    } void Add (int u, int v) {edge[rear].v = v;
    EDGE[REAR].NXT = Head[u];
Head[u] = rear++; } int find (int x) {return p[x] = = X ×: (P[x] = find (p[x))} int find1 (int x) {return p1[x] = = x: (p1[x
] = Find1 (p1[x]);  } int Find2 (int x) {return p2[x] = = X ×: (P2[x] = Find2 (p2[x])), bool Solve (Edge e[], int head[], int px[], char
        OP) {for (int i = 0; i < n; i++) {b[i].reset ();
        B[i][i]=1;
        Vis[i] = 0;
    P[i] = Px[i]; } queue<int>q;
    for (int i = 0; i < n; i++) if (find (i) = = i) {Q.push (i); vis[i] = 1;
        } while (!q.empty ()) {int u = q.front (); Q.pop ();
        Vis[u] = 0;
            for (int i = head[u]; ~i; i = e[i].nxt) {int v = E[I].V;
                if ((B[v] | b[u])! = B[v]) {B[v] |= b[u];
                    if (!vis[v]) {Q.push (v);
                VIS[V] = 1;
            }}}} for (int i = 0, i < n; i++) {for (int j = 0; J < N; j + +) {
            if (i = = j) Continue;
        if (B[i][j] && mp[j][i]! = OP) return false;
}} return true;
    } int main () {int T;
    Freopen ("In.txt", "R", stdin);
    scanf ("%d", &t);
        while (t--) {scanf ("%d", &n);
        Init ();
        for (int i = 0; i < n; i++) scanf ("%s", &mp[i]);
              for (int i = 0, i < n; i++) {for (int j = 0; J < N; j + +) {  if (mp[i][j] = = ' P ') {ADD (i, j);
                    int pi = Find1 (i), PJ = Find1 (j);
                if (pi! = PJ) P1[PJ] = pi;
                    } if (mp[i][j] = = ' Q ') {Add (I, j);
                    int pi = Find2 (i), PJ = Find2 (j);
                if (pi! = PJ) P2[PJ] = pi;
        }}} if (Solve (E, head, p1, ' P ') && Solve (edge, head, p2, ' Q ')) printf ("t\n");
    else printf ("n\n");
} return 0;
 }



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.