码迷,mamicode.com
首页 > 其他好文 > 详细

Bit Magic HDU - 4421

时间:2017-12-07 18:50:34      阅读:241      评论:0      收藏:0      [点我收藏+]

标签:about   multi   table   nbsp   output   string   ret   line   inter   

Yesterday, my teacher taught me about bit operators: and (&), or (|), xor (^). I generated a number table a[N], and wrote a program to calculate the matrix table b[N][N] using three kinds of bit operator. I thought my achievement would get teacher‘s attention.
The key function is the code showed below.
技术分享图片

There is no doubt that my teacher raised lots of interests in my work and was surprised to my talented programming skills. After deeply thinking, he came up with another problem: if we have the matrix table b[N][N] at first, can you check whether corresponding number table a[N] exists?

InputThere are multiple test cases.
For each test case, the first line contains an integer N, indicating the size of the matrix. (1 <= N <= 500).
The next N lines, each line contains N integers, the jth integer in ith line indicating the element b[i][j] of matrix. (0 <= b[i][j] <= 2 31 - 1)
OutputFor each test case, output "YES" if corresponding number table a[N] exists; otherwise output "NO".Sample Input

2
0 4
4 0
3
0 1 24
1 0 86
24 86 0

Sample Output

YES
NO
  1 // ConsoleApplication2.cpp: 定义控制台应用程序的入口点。
  2 //
  3 
  4 // ConsoleApplication1.cpp: 定义控制台应用程序的入口点。
  5 //
  6 
  7 #include "stdafx.h"
  8 #include<stack>
  9 #include<cstdio>
 10 #include<cstring>
 11 #include<iostream>
 12 #include<algorithm>
 13 #define mem(array) memset(array,0,sizeof(array))
 14 using namespace std;
 15 
 16 const int maxn = 6000;
 17 const int maxm = 999999;
 18 
 19 stack<int> S;
 20 
 21 struct node {
 22     int to, next;
 23 }e[maxm];
 24 
 25 int k, n, tot, index;
 26 int Low[maxn], Dfn[maxn], head[maxn], cmp[maxn],map[505][505];
 27 bool use[maxn];
 28 
 29 void Inite() {
 30     k = tot = index = 0;
 31     mem(Dfn); mem(use); mem(cmp);
 32     memset(head, -1, sizeof(head));
 33 }
 34 
 35 void addedge(int u, int v) {
 36     e[tot].to = v;
 37     e[tot].next = head[u];
 38     head[u] = tot++;
 39 }
 40 
 41 void Tarjan(int u) {
 42     Low[u] = Dfn[u] = ++index;
 43     S.push(u);
 44     use[u] = 1;
 45     for (int i = head[u]; i != -1; i = e[i].next) {
 46         int v = e[i].to;
 47         if (!Dfn[v]) {
 48             Tarjan(v);
 49             Low[u] = min(Low[u], Low[v]);
 50         }
 51         else if (use[v]) {
 52             Low[u] = min(Low[u], Dfn[v]);
 53         }
 54     }
 55     int v;
 56     if (Dfn[u] == Low[u]) {
 57         k++;
 58         do {
 59             v = S.top();
 60             S.pop();
 61             cmp[v] = k;
 62             use[v] = 0;
 63         } while (v != u);
 64     }
 65 }
 66 
 67 bool check() {
 68     for (int i = 0; i < n; i++) {
 69         if (map[i][i]) return false;
 70         for (int j = i + 1; j < n; j++)
 71             if (map[i][j] != map[j][i]) return false;
 72     }
 73     return true;
 74 }
 75 
 76 int main()
 77 {
 78     while (scanf_s("%d", &n) != EOF) {
 79         for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) scanf_s("%d", &map[i][j]);
 80         if (!check()) { puts("NO"); continue; }
 81         bool flag = false;
 82         for (int p = 0; p < 31; p++) {
 83             Inite();
 84             for (int i = 0; i < n; i++) {
 85                 for (int j = i + 1; j < n; j++) {
 86                     if (i % 2 == 1 && j % 2 == 1) {
 87                         if ((map[i][j] >> p) & 1) {
 88                             addedge(i + n, j);
 89                             addedge(j + n, i);
 90                         }
 91                         else {
 92                             addedge(i, i + n);
 93                             addedge(j, j + n);
 94                         }
 95                     }
 96                     else if (i % 2 == 0 && j % 2 == 0) {
 97                         if ((map[i][j] >> p) & 1) {
 98                             addedge(i + n, i);
 99                             addedge(j + n, j);
100                         }
101                         else {
102                             addedge(i, j + n);
103                             addedge(j, i + n);
104                         }
105                     }
106                     else {
107                         if ((map[i][j] >> p) & 1) {
108                             addedge(i, j + n);
109                             addedge(i + n, j);
110                             addedge(j, i + n);
111                             addedge(j + n, i);
112                         }
113                         else {
114                             addedge(i, j);
115                             addedge(j, i);
116                             addedge(i + n, j + n);
117                             addedge(j + n, i + n);
118                         }
119                     }
120                 }    
121             }
122             for (int i = 0; i < 2 * n; i++) if (!Dfn[i]) Tarjan(i);
123             for (int i = 0; i < n; i++) if (cmp[i] == cmp[i + n]) flag = true;
124             if (flag) break;    
125          }
126         if (flag) puts("NO");
127         else puts("YES");
128     }
129     return 0;
130 }

 

Bit Magic HDU - 4421

标签:about   multi   table   nbsp   output   string   ret   line   inter   

原文地址:http://www.cnblogs.com/zgglj-com/p/8000115.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!