2018.1.21【POJ - 2260】解題報告(模擬,圖論引申)

Error Correction
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 6820   Accepted: 4286

Description

A boolean matrix has the parity property when each row and each column has an even sum, i.e. contains an even number of bits which are set. Here's a 4 x 4 matrix which has the parity property: 
1 0 1 0

0 0 0 0

1 1 1 1

0 1 0 1

The sums of the rows are 2, 0, 4 and 2. The sums of the columns are 2, 2, 2 and 2. 
Your job is to write a program that reads in a matrix and checks if it has the parity property. If not, your program should check if the parity property can be established by changing only one bit. If this is not possible either, the matrix should be classified as corrupt. 

Input

The input will contain one or more test cases. The first line of each test case contains one integer n (n<100), representing the size of the matrix. On the next n lines, there will be n integers per line. No other integers than 0 and 1 will occur in the matrix. Input will be terminated by a value of 0 for n.

Output

For each matrix in the input file, print one line. If the matrix already has the parity property, print "OK". If the parity property can be established by changing one bit, print "Change bit (i,j)" where i is the row and j the column of the bit to be changed. Otherwise, print "Corrupt".

Sample Input

4
1 0 1 0
0 0 0 0
1 1 1 1
0 1 0 1
4
1 0 1 0
0 0 1 0
1 1 1 1
0 1 0 1
4
1 0 1 0
0 1 1 0
1 1 1 1
0 1 0 1
0

Sample Output

OK
Change bit (2,3)
Corrupt

Source


【題目大意】

給定一個n*n的由0/1構成的boolean矩陣,判斷是否各行各列和均爲偶數,若是輸出OK,若不是,可否更改其中一點的位置使行列均爲偶數。輸出這個點,若不可以,輸出Corrupt

【解題思路】

改一個點即可的充要條件爲行和列之中存在且僅有一行與一列的各自的和爲奇數。

或者利用鄰接矩陣的知識轉化爲有向圖+深度優先搜索??學姐講的圖論知識,還沒有深入學習。

【解題代碼】

#include <cstdio>
#include <cstring>
#define maxn 102
int map[maxn][maxn];
using namespace std;
int main()
{
	int n;
	while(scanf("%d",&n)&&n)
	{
		memset(map,0,sizeof(map));
		int sumh=0,sumv=0;
		for(int i=1;i<=n;i++)
		{
			for(int j=1;j<=n;j++)
			{
				scanf("%d",&map[i][j]);
				map[n+1][j]+=map[i][j];
				map[i][n+1]+=map[i][j];
			}
		}
				//solve it
		int marka=0,markb=0,ans=0,bns=0;
		for(int i=1;i<=n;i++)
		{
			if(map[n+1][i]%2)
			{
				bns++;
				markb=i;
			}
			if(map[i][n+1]%2)
			{
				ans++;
				marka=i;
			}
		}
		if(!ans&&!bns)	printf("OK\n");
		else if(ans==1&&bns==1)	printf("Change bit (%d,%d)\n",marka,markb);
		else printf("Corrupt\n");
		map[marka][markb]=!map[marka][markb];
		
		 
	}
}

【收穫與反思】

圖論知識補充:

1.我們的定義的數據對象的範圍是有向圖和無向圖
2.圖的通路:
圖中的頂點與邊的交替序列我們稱之爲是圖的通路
3.歐拉通路:
我們從有向圖或者無向圖中的任意一點出發,將所有的邊遍歷且僅遍歷一次的通路序列我們稱之爲是歐拉通路
4.歐拉回路:
如果我們的歐拉通路的起點和終點是一樣的我們稱之爲歐拉回路
5.歐拉圖:
具有歐拉回路的圖稱之爲歐拉圖,規定平凡圖(只有一個頂點的空圖)屬於歐拉圖
6.半歐拉圖:
具有歐拉通路的圖我們稱之爲是半歐拉圖

一些相關定理:

1.無向連通圖 G 是歐拉圖,當且僅當 G 不含奇數度結點( G 的所有結點度數爲偶數);
2.無向連通圖G 含有歐拉通路,當且僅當 G 有零個或兩個奇數度的結點;
3.有向連通圖 D 是歐拉圖,當且僅當該圖爲連通圖且 D 中每個結點的入度=出度;
4.有向連通圖 D 含有歐拉通路,當且僅當該圖爲連通圖且 D 中除兩個結點外,其餘每個結點的入度=出度,且此兩點滿足 deg-(u)-deg+(v)=±1 。(起始點s的入度=出度-1,結束點t的出度=入度-1 或兩個點的入度=出度);
5.一個非平凡連通圖是歐拉圖當且僅當它的每條邊屬於奇數個環;
6.如果圖G是歐拉圖且 H = G-uv,則 H 有奇數個 u,v-跡僅在最後訪問 v ;同時,在這一序列的 u,v-跡中,不是路徑的跡的條數是偶數。

發佈了40 篇原創文章 · 獲贊 1 · 訪問量 6257
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章