Uva11464 Even Parity

枚举每个格子的状态显然是不可能的。

思考发现,矩阵第一行的状态确定以后,下面的状态都可以递推出来。

于是状压枚举第一行的状态,递推全图的状态并判定是否可行。

 1 /*by SilverN*/
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<cstring>
 5 #include<cstdio>
 6 #include<cmath>
 7 #include<list>
 8 using namespace std;
 9 int read(){
10     int x=0,f=1;char ch=getchar();
11     while(ch<‘0‘ || ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
12     while(ch>=‘0‘ && ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();}
13     return x*f;
14 }
15 struct node{
16     int pre,nxt;
17 }a[1000010];
18 int link[1000010];
19 int ud[1000010];
20 int L,P;
21 void Del(int x){
22     ud[x]=0;
23     a[a[x].pre].nxt=a[x].nxt;
24     a[a[x].nxt].pre=a[x].pre;
25 }
26 int T;
27 int main(){
28     T=read();
29     int L,P,i,j;
30     for(int cas=1;cas<=T;cas++){
31         memset(link,0,sizeof link);
32         memset(ud,0,sizeof ud);
33         L=read();P=read();
34         for(i=0;i<L;i++){
35             a[i].pre=i-1;
36             a[i].nxt=i+1;
37         }
38         a[0].pre=L-1;a[L-1].nxt=0;
39         int u,v;
40         for(i=1;i<=P;i++){
41             u=read();v=read();
42             link[u]=v;link[v]=u;
43             ud[u]=1;ud[v]=-1;//????
44         }
45         for(i=0;i<L;i++)if(!ud[i])Del(i);
46         int hd=0;
47         while(P){
48             bool flag=1;
49             while(!ud[hd])hd++;
50             for(i=a[hd].nxt; i!=hd && flag; i=a[i].nxt){
51                 int u=i,v=a[i].nxt;
52                 if(ud[u]==ud[v] && (a[link[u]].nxt==link[v] ||
53                     a[link[v]].nxt==link[u])){
54                         Del(u);    Del(v);
55                         Del(link[u]);
56                         Del(link[v]);
57                         P-=2;
58                         flag=0;
59                     }
60                 //passing
61                 else if(link[v]==u || link[u]==v){
62                     Del(u);Del(v);
63                     P--;
64                     flag=0;
65                 }
66                 //selfloop
67             }
68             if(flag)break;
69         }
70         printf("Case #%d: ",cas);
71         if(!P)printf("YES\n");
72         else printf("NO\n");
73     }
74     return 0;
75 }
时间: 2024-08-28 05:26:51

Uva11464 Even Parity的相关文章

UVa11464 Even Parity (枚举子集)

链接:http://acm.hust.edu.cn/vjudge/problem/24665分析:枚举第0行的情况,接下来可以根据第0行计算出第1行,根据第1行计算出第2行...方法是当确定B[r][c]是0还是1时(前提要合法),根据B[r-1][c]的上,左,右3个元素之和来判断,如果出现将原来是1的变为0则不合法返回INF,最后统计下原来是0后来变为1的个数cnt,然后返回更新ans被改变元素的最小个数即可. 1 #include <cstdio> 2 #include <cstr

【枚举暴力】【UVA11464】 Even Parity

传送门 Description 给你一个0/1矩阵,可以将矩阵中的0变成1,问最少经过多少此操作使得矩阵任意一元素四周的元素和为偶数. Input 第一行是一个整数T代表数据组数,每组数据包含以下内容: 第一行是一个整数n,代表矩阵的行列数 接下来n行每行n个用空格隔开的整数,代表矩阵元素. Output 对于每组数据输出一行,格式为Case X: ans Sample Input 3 3 0 0 0 0 0 0 0 0 0 3 0 0 0 1 0 0 0 0 0 3 1 1 1 1 1 1 0

Parity Game CodeForces - 298C

You are fishing with polar bears Alice and Bob. While waiting for the fish to bite, the polar bears get bored. They come up with a game. First Alice and Bob each writes a 01-string (strings that only contain character "0" and "1") a an

Parity

Parity Time limit: 2.0 secondMemory limit: 64 MB Now and then you play the following game with your friend. Your friend writes down a sequence consisting of zeroes and ones. You choose a continuous subsequence (for example the subsequence from the th

Codeforces 549C The Game Of Parity(博弈)

The Game Of Parity Solution: 这个题只需要分类讨论就可以解决. 先分别统计奇数和偶数的个数. 然后判断谁走最后一步,如果走最后一步时候同时有偶数和奇数,那么走最后一步的赢.如果没有呢,就看剩下的是奇数还是偶数,是偶数不用说,是奇数看剩奇数个还是偶数个. 针对这几种情况讨论一下就可以. #include <bits/stdc++.h> using namespace std; int n, k, o, e; int main() { cin >> n &g

poj 1733 Parity game

Parity game 题意:一个长度为N(N < 1e9)内的01串,之后有K(K <= 5000)组叙述,表示区间[l,r]之间1的个数为odd还是even:问在第一个叙述矛盾前说了几句话? Sample Input 10 N 5 K 1 2 even 3 4 odd 5 6 even 1 6 even (错误) 7 10 odd Sample Output 3 思路:对于判断正误的问题,一定要知道什么情况会导致错误.如样例:当前输入的区间[1,6]的端点均包含于前面输入的端点中时(包含并

Codeforces #180 div2 C Parity Game

// Codeforces #180 div2 C Parity Game // // 这道题的题目意思就不解释了 // // 题目有那么一点难(对于我而言),不多说啦 // // 解题思路: // // 首先如果a串和b串相等,不多说直接YES // 如果b串全是0,直接YES // 注意到a串有一个性质,1的个数不会超过本身的加1. // a有个1的上限设为x,b有个1的个数设为y,则如果x < y // 那么直接NO. // // 现在一般情况下,就是模拟啦,找到a的后缀和b的前缀一样的

[2016-03-18][POJ][1733][Parity game]

时间:2016-03-18 09:55:52 星期五 题目编号:[2016-03-18][POJ][1733][Parity game] 题目大意:给定若干的区间范围内的数字的和,问从哪句开始是错误的 分析: 带权并查集 区间长度高达1000000000显然不可能直接建立数组,但是发现询问只有5000次,也就是最多出现了5000*2个点,离散化就可以解决问题 relation[i] i为区间的左端点,fa[i]为区间的右端点,relation[i]维护(i,fa[i])这段区间的和的奇偶状态,0

51nod 1204 Parity(并查集应用)

1204 Parity 题目来源: Ural 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 你的朋友写下一串包含1和0的串让你猜,你可以从中选择一个连续的子串(例如其中的第3到第5个数字)问他,该子串中包含了奇数个还是偶数个1,他会回答你的问题,然后你可以继续提问......你怀疑朋友的答案可能有错,或说同他之前的答案相互矛盾,例如:1 - 2 奇数,3 - 4 奇数,那么可以确定1 - 4 一定是偶数,如果你的朋友回答是奇数,就产生了矛盾.给出所有你朋友的