poj 1733Parity game(map離散+帶權並查集)
Time Limit: 1000MS | Memory Limit: 65536K |
Total Submissions: 7567 | Accepted: 2951 |
Description
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 third to the fifth digit inclusively) and ask him, whether this subsequence contains even or odd number of ones. Your friend answers your question and you can ask him about another subsequence and so on. Your task is to guess the entire sequence of numbers.You suspect some of your friend's answers may not be correct and you want to convict him of falsehood. Thus you have decided to write a program to help you in this matter. The program will receive a series of your questions together with the answers you have received from your friend. The aim of this program is to find the first answer which is provably wrong, i.e. that there exists a sequence satisfying answers to all the previous questions, but no such sequence satisfies this answer.
Input
Output
Sample Input
10 5 1 2 even 3 4 odd 5 6 even 1 6 even 7 10 odd
Sample Output
3
題目:有一個長度 已知的01串,給出多個條件,[l,r]這個區間中1的個數是奇數還是偶數,問前幾個是正確的,沒有矛盾
[l,r]中1個個數可以表示為sum[r]-sum[l-1],而題目只要求是奇偶,也就確定了sum[r]與sum[l-1]奇偶是否相同,到了這步就簡單了,轉變為經典模型
範圍有點大,按l-1,r離散化一下
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <string>
#include <cmath>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <stack>
#include <map>
using namespace std;
#define esp 1e-8
const double PI = acos(-1.0);
const long long inf = 1000000000;
const long long mod = 10000007;
typedef long long LL;
#pragma comment(linker, "/STACK:1024000000,1024000000")
//freopen("in.txt","r",stdin); //輸入重定向,輸入資料將從in.txt檔案中讀取
//freopen("out.txt","w",stdout); //輸出重定向,輸出資料將儲存在out.txt檔案中
int f[30005];int v[30005];
int a[30005], b[30005];
char s[30005][10];
set<int>st;
set<int>::iterator it;
map<int, int>mp;
int find(int x)
{
if (x != f[x])
{
int u = f[x];
f[x] = find(f[x]);
v[x] = (v[u] + v[x]) % 2;
}
return f[x];
}
int main()
{
int n, m, i, j;
while (~scanf("%d%d", &n, &m))
{
int ans = 0;
int res = 0;
st.clear();
mp.clear();
for (i = 1; i <= m; ++i)
{
scanf("%d%d%s", &a[i], &b[i], s[i]);
st.insert(a[i]);
st.insert(b[i]);
}
int nn = 0;
for (it = st.begin(); it != st.end(); ++it)
{
mp[*it] = ++nn;
}
for (i = 0; i <= nn; ++i)
f[i] = i;
memset(v, 0, sizeof(v));
v[0] = 0;
for (i = 1; i <= m; ++i)
{
int fa = find(mp[a[i]] - 1);
int fb = find(mp[b[i]]);
if (fa == fb)//如果相同,則已經知道了mp[a[i]] - 1和mp[b[i]]的奇偶,所以直接判斷即可
{
if (v[mp[a[i]] - 1] == v[mp[b[i]]] && s[i][0] == 'o')
break;
if (v[mp[a[i]] - 1] != v[mp[b[i]]] && s[i][0] == 'e')
break;
ans++;
}
else
{
f[fa] = fb;
if (s[i][0] == 'o') //如果是奇數,需要加上1
{
v[fa] = (v[mp[a[i]] - 1] + v[mp[b[i]]] + 1) % 2;
}
else
{
v[fa] = (v[mp[a[i]] - 1] + v[mp[b[i]]]) % 2;
}
ans++;
}
}
printf("%d\n", ans);
}
}
相關推薦
poj 1733Parity game(map離散+帶權並查集)
Parity game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7567 Accepted: 2951 Description Now and then you play the foll
POJ 1182 食物鏈 (帶權並查集)
動物 return i++ rip width d+ oid body 思維 食物鏈 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 78551 Accepte
POJ 1988 Cube Stacking (帶權並查集)
ons from += same file rep stream scribe 並查集 Farmer John and Betsy are playing a game with N (1 <= N <= 30,000)identical cubes label
Cube Stacking 【POJ - 1988】【並查集(帶權並查集)】
題目連結 題意就是給你一串箱子,題目中沒有給出箱子的數量,問你經過一系列移動之後,某個ID箱子下面有幾個箱子,然後移動是把這一整列都按照原來的順序放在另一列的頂端上去。 所以,我想到了用並查集來寫,對於一個箱子,我們把它放到另一列箱子的上面,我們可以維護下
POJ 2492 A Bug's Life (帶權並查集)
A Bug's Life Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 45943  
POJ 1182 食物鏈 (經典帶權並查集)
第三次複習了,最經典的並查集 題意:動物王國中有三類動物A,B,C,這三類動物的食物鏈構成了有趣的環形。A吃B, B吃C,C吃A。 現有N個動物,以1-N編號。每個動物都是A,B,C中的一種,但是我們並不知道它到底是哪一種。 有人用兩種說法對這N個動物
HDU3635 Dragon Balls(帶權並查集)
align size any cat webkit city ret follow num 題目鏈接: http://acm.hdu.edu.cn/showproblem.php?pid=3635 題目描述: Dragon Balls Problem Descript
並查集——poj2236(帶權並查集)
algorithm blog oid poj2236 stream 命令 net 最大 spa 題目:Wireless Network 題意:給定n臺已損壞計算機的位置和計算機最遠通信距離d,然後分別根據命令執行以下兩種操作: "O p" (1 <= p <=
HDU - 3038 / 3048 (帶權並查集) (待補)
hdu return bsp value 題目 ios i++ mes sca 題目鏈接:點我點我 題意: 題解: 兩題代碼差不多,放個3047的。 1 #include <cstdio> 2 #include <iostream>
HDU 3038 How Many Answers Are Wrong(帶權並查集)
define pro tor memset set sizeof fin printf class 題目鏈接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1272 題目大意:有n條信息,每條信息都給出區間l到r的值,如果
poj1703 Find them, Catch them(帶權並查集)
har find 輸出 scanf -- oot n) puts stdin 題目鏈接 http://poj.org/problem?id=1703 題意 有兩個幫派:龍幫和蛇幫,兩個幫派共有n個人(編號1~n),輸入m組數據,每組數據為D [a][b]或A [a][
poj2492 A Bug's Life(帶權並查集)
tab set 復雜 鏈接 image 之間 判斷 clu 成了 題目鏈接 http://poj.org/problem?id=2492 題意 蟲子有兩種性別,有n只蟲子,編號1~n,輸入m組數據,每組數據包含a、b兩只蟲子,表示a、b為不同性別的蟲子,根據輸入的m組
poj1182 食物鏈(帶權並查集)
txt amp 帶權並查集 htm 代碼 || blog 參考 oid 題目鏈接 http://poj.org/problem?id=1182 思路 前面做的帶權並查集的權值記錄該結點與其父結點是否是同一類,只有兩種取值情況(0,1),在這題中某結點a和其父結點b的取
HihoCoder - 1515 分數調查 (帶權並查集)
amp code space blog contain script += esp 判斷 小Hi的學校總共有N名學生,編號1-N。學校剛剛進行了一場全校的古詩文水平測驗。 學校沒有公布測驗的成績,所以小Hi只能得到一些小道消息,例如X號同學的分數比Y號同學的分數高S分
食物鏈(帶權並查集)
clu style org 之間 i++ init esc rip 但是 題目鏈接:http://poj.org/problem?id=1182 題目: Description 動物王國中有三類動物A,B,C,這三類動物的食物鏈構成了有趣的環形。A吃B, B吃C,C吃
HDU1289 A Bug's Life (帶權並查集)
clu some assume ever different 合並操作 ports main separate HDU1289 帶權並查集 Problem Description Background Professor Hopper is researching the
銀河英雄傳說(帶權並查集)
lse space std 如果 print code i++ int 維護 題意就是要維護並查集,切詢問兩個點是不是在一棵樹上,如果是,輸出他們間的數減一 #include<iostream> #include<cstdio> using name
How Many Answers Are Wrong(帶權並查集)
題目: TT and FF are … friends. Uh… very very good friends -________-b FF is a bad boy, he is always wooing TT to play the following game with him.
BZOJ1202:狡猾的商人(帶權並查集)
else class puts tex algorithm str 修改 任務 scrip 1202: [HNOI2005]狡猾的商人 題目鏈接:https://www.lydsy.com/JudgeOnline/problem.php?id=1202 Descriptio
POJ1417:True Liars(DP+帶權並查集)
True Liars Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 16338 &n