zoukankan      html  css  js  c++  java
  • hdu3234 Exclusive-OR

    Description

    You are not given (n) non-negative integers (X_0, X_1, cdots, X_{n-1}) less than (2^{20}) , but they do exist, and their values never change.

    I'll gradually provide you some facts about them, and ask you some questions.

    There are two kinds of facts, plus one kind of question:

    Input

    There will be at most (10) test cases. Each case begins with two integers (n) and (Q (1 le n le 20000, 2 le Q le 40000)) . Each of the following lines contains either a fact or a question, formatted as stated above. The (k) parameter in the questions will be a positive integer not greater than (15) , and the (v) parameter in the facts will be a non-negative integer less than (2^{20}). The last case is followed by (n=Q=0) , which should not be processed.

    Output

    For each test case, print the case number on its own line, then the answers, one on each one. If you can't deduce the answer for a particular question, from the facts I provide you before that question, print "I don't know.", without quotes. If the (i)-th fact (don't count questions) cannot be consistent with all the facts before that, print "The first i facts are conflicting.", then keep silence for everything after that (including facts and questions). Print a blank line after the output of each test case.

    Sample

    Sample Input

    2 6
    I 0 1 3
    Q 1 0
    Q 2 1 0
    I 0 2
    Q 1 1
    Q 1 0
    3 3
    I 0 1 6
    I 0 2 2
    Q 2 1 2
    2 4
    I 0 1 7
    Q 2 0 1
    I 0 1 8
    Q 2 0 1
    0 0
    

    Sample Output

    Case 1:
    I don't know.
    3
    1
    2
    Case 2:
    4
    Case 3:
    7
    The first 2 facts are conflicting.
    

    Solution

    沉迷水题。

    带权并查集。

    #include<bits/stdc++.h>
    using namespace std;
    
    #define N 20001
    #define rep(i, a, b) for (int i = a; i <= b; i++)
    
    inline int read() {
    	int x = 0, flag = 1; char ch = getchar(); while (!isdigit(ch)) { if (!(ch ^ '-')) flag = -1; ch = getchar(); }
    	while (isdigit(ch)) x = (x << 1) + (x << 3) + ch - '0', ch = getchar(); return x * flag;
    }
    
    int n, Q, fa[N], num[N], val[N];
    
    int find(int x) {
    	if(x == fa[x]) return x;
    	int t = fa[x]; fa[x] = find(fa[x]), val[x] ^= val[t]; return fa[x];
    }
    
    bool merge(int u, int v, int x) {
    	int fu = find(u), fv = find(v); if(fu == fv) return (val[u] ^ val[v]) == x;
    	if(fu == n) swap(fu, fv); fa[fu] = fv, val[fu] = val[u] ^ val[v] ^ x; return 1;
    }
    
    int main() {
    	for (int T = 1; ; T++) {
    		n = read(), Q = read(); if (!n && !Q) break; printf("Case %d:
    ", T);
    		rep(i, 0, n) fa[i] = i, val[i] = 0;
    		int facts = 0; bool flag = 1; char op[2], str[20];
    		while (Q--) {
    			if (!flag) { gets(str); continue; }
    			scanf("%s", op);
    			if(op[0] == 'I') {
    				gets(str), facts++; int u, v, x;
    				if(sscanf(str, "%d%d%d", &u, &v, &x) == 2) x = v, v = n;
    				if(!merge(u, v, x)) printf("The first %d facts are conflicting.
    ", facts), flag = 0;
    			}
    			else {
    				int k = read(), x[20], ans = 0; bool tag = 1;
    				rep(i, 1, k) x[i] = read(), num[find(x[i])] = 0;
    				rep(i, 1, k) num[find(x[i])]++, ans ^= val[x[i]];
    				rep(i, 1, k) if(num[find(x[i])] % 2 == 1 && find(x[i]) != n) { tag = 0; break; }
    				if(!tag) puts("I don't know."); else printf("%d
    ", ans);
    			}
    		}
    	}
    	return 0;
    }
    
  • 相关阅读:
    Python-HTML基础
    异常处理
    反射hasattr; getattr; setattr; delattr
    Python 属性方法、类方法、静态方法、 特殊属性__doc__ (内建属性)
    Python3 day6面向对象
    re模块计算器作业
    re正则表达式:import re ;re.search()
    hashlib模块学习:hmac
    ConfigParser模块,主要应用于对php.ini等格式的配置文件内容读取和生成。删改较少用
    ymal文档格式 处理
  • 原文地址:https://www.cnblogs.com/aziint/p/8558453.html
Copyright © 2011-2022 走看看