poj 2345 Central heating

Central heating
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 678   Accepted: 310

Description

Winter has come, but at the Ural State University heating is not turned on yet. There's one little problem: the University is heated only if all of the valves are opened. There are some technicians at the University. Each of them is responsible for one or more valves. There may be several technicians responsible for the same valve. When a technician gets an instruction to turn on the heating he goes round all of his valves and turns them. It means that if a valve was opened then he closes it, and if it was closed then he opens it. It is well known that every technician earns his money not in vain so it's impossible to replace any technician by any combination of other technicians. 
Your task is to determine who of the technicians is to get an instruction "to turn on the heating" in order to heat all the Ural State University. Note that there are N technicians and N valves at the University (1 <= N <= 250). 

Input

The first line of an input contains the number N. The next N lines contain lists of the valves in charge of each of the technicians. It means that a line number i + 1 contains numbers of the valves that the i-th technician is responsible for. Each list of valves is followed by –1.

Output

An output should contain a list of technicians' numbers sorted in ascending order. If several lists are possible, you should send to an output the shortest one. If it's impossible to turn on the heating at the University, you should write "No solution" .

Sample Input

4
1 2 -1
2 3 4 -1
2 -1
4 -1

Sample Output

1 2 3

Source

/*
* @Author: Lyucheng
* @Date:   2017-08-09 16:37:47
* @Last Modified by:   lyuc
* @Last Modified time: 2017-08-14 11:31:17
*/

/*
 题意:有n个窗口,有n个师傅,每个师傅可以管理很多窗口,可以重复的,给一个师傅下命令,这个师傅会管理他所有的
    窗口,如果是关闭的就打开,如果是打开的就关闭,现在想让所有的窗口打开,问你怎么下命令

 思路:将每个师傅管理的窗口转化成01序列,末尾加0,表示方程的结果,然后凑成矩阵,高斯消元解方程,消元的时候
    变成异或
*/

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>

#define MAXN 255

using namespace std;

int n;
int cur;
int a[MAXN][MAXN];//增广矩阵
int x[MAXN];//解集

void Gauss(){
    for(int i=0;i<n;i++){
        int col=-1;
        for(int j=i;j<n;j++){//找到第一个不为零的行
            if(a[j][i]){
                col=j;
                break;
            }
        }
        for(int j=i;j<=n;j++){
            swap(a[i][j],a[col][j]);
        }
        for(int j=i+1;j<n;j++){
            if(a[j][i]!=0){
                for(int k=i;k<=n;k++){
                    a[j][k]^= a[i][k];
                }
            }
        }
    }
    for (int i=n-1;i>=0;--i){
        x[i] = a[i][n];
        for (int j=i-1;j>=0;--j){
            a[j][n]^=(x[i]&a[j][i]);
        }
    }
}

inline void init(){
    memset(a,0,sizeof a);
    memset(x,0,sizeof x);
}

int main(){ 
    // freopen("in.txt", "r", stdin);
    // freopen("out.txt", "w", stdout);
    while(scanf("%d",&n)!=EOF){
        init();
        for(int i=0;i<n;i++){
            while(scanf("%d",&cur)&&cur!=-1){
                a[cur-1][i]=1;
            }
            a[i][n]=1;
        }

        Gauss();
        bool flag=true;
        for (int i=0;i<n;i++){
            if(x[i]){
                if(flag==true){
                    flag=false;
                    printf("%d",i+1);
                }else{
                    printf(" %d",i+1);
                }
            }
        }
        printf("\n");
    }
    return 0;
}

 

posted @ 2017-08-14 11:32  勿忘初心0924  阅读(260)  评论(0编辑  收藏  举报