HDU 5961 Delivery (brute force search)

Source: Internet
Author: User

We call a graph G to be passed , and if and only if there is an edge from A to B in G and an edge from B to C, if and only for any three different vertex A, then there is also an edge from A to C in G.

We call Figure G a contest chart , when 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.
A competition chart with 4 vertices is displayed.

Now, give you two of the graph P = (V,EP ) and q = (V,ee ) To meet:
1.EP Andee no public side;
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 graph Q.
? Otherwise there are no edges from I to j in two graphs.
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).

Test instructions: It is not explained anyway is Chinese.

is to traverse all the edges and then determine whether it meets the conditions of the problem, simple violence to the time of the card, you can use vectors to save the adjacent edge so much faster.

The first one with the vector, the second with the structure of the comparison card time, the side of the more dense words or with the structure of a better use of the vector relatively faster .

#include <iostream> #include <cstring> #include <cstdio> #include <vector>using namespace std; const int M = 2e3 + 20;char sl[m][m];int n;vector<int>q[m], p[m];void init () {for (int i = 0; I <= N; i++)        {q[i].clear ();    P[i].clear ();    }}int dfs (int x, vector<int> g[], char cp) {int fi = G[x].size ();        for (int i = 0; I < fi; i++) {int d = g[x][i];        int se = g[d].size ();            for (int j = 0; J < se; j + +) {int k = g[d][j];            if (sl[x][k]! = CP) {return 0; }}} return 1;}    int main () {int t;    scanf ("%d", &t);        while (t--) {scanf ("%d", &n);        Init ();            for (int i = 1; I <= n; i++) {scanf ("%s", Sl[i] + 1);                for (int j = 1; J <= N; j + +) {if (sl[i][j] = = ' P ') P[i].push_back (j);   if (sl[i][j] = = ' Q ') Q[i].push_back (j);         }} int flag = 0;                for (int i = 1; I <= n; i++) {if (!dfs (I, p, ' P ')) {flag = 1;            Break                    }} if (!flag) {for (int i = 1; I <= n; i++) {if (!dfs (i, Q, ' Q ')) {                    flag = 1;                Break        }}} if (flag) printf ("n\n");    else printf ("t\n"); } return 0;}
#include <iostream> #include <cstring> #include <cstdio>using namespace std;const int M = 3e3 + 10;char SL M [M];int N, E, head1[m], head2[m];struct ss {int V, next;}    A[m * M], b[m * m];void init () {e = 0;        for (int i = 0; I <= n + 1; i++) {Head1[i] =-1;    Head2[i] =-1;    }}void Add (int x, int y, SS s[], int head[]) {s[e].v = y;    S[e].next = Head[x]; HEAD[X] = e++;} int dfs (int t, char CP, int head[], SS s[]) {for (int i = head[t]; I! =-1; i = S[i].next) {for (int j = Hea D[S[I].V]; J! =-1;            j = s[j].next) {if (sl[t][s[j].v] = = CP);        else return 0; }} return 1;}    int main () {int t;    scanf ("%d", &t);        while (t--) {scanf ("%d", &n);        Init ();            for (int i = 1; I <= n; i++) {scanf ("%s", Sl[i] + 1); for (int j = 1; J <= N; j + +) {if (sl[i][j] = = ' P ') add(I, J, A, head1);            if (sl[i][j] = = ' Q ') Add (i, J, B, head2);        }} int flag = 0;                for (int i = 1; I <= n; i++) {if (!dfs (i, ' P ', Head1, a)) {flag = 1;            Break                     }} if (!flag) {for (int i = 1; I <= n; i++) {if (!dfs (i, ' Q ', head2, b)) {                    flag = 1;                Break        }}} if (flag) printf ("n\n");    else printf ("t\n"); } return 0;}

Hdu 5961 Delivery (brute force search)

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.