Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 765 | Accepted: 371 |
Description
Input
Accepted answers to the poll question | Encoding |
I would be happy if at least one from i and j is elected. | +i +j |
I would be happy if at least one from i and j is not elected. | -i -j |
I would be happy if i is elected or j is not elected or both events happen. | +i -j |
I would be happy if i is not elected or j is elected or both events happen. | -i +j |
The input data are separated by white spaces, terminate with an end of file, and are correct.
Output
Sample Input
3 3 +1 +2 -1 +2 -1 -3 2 3 -1 +2 -1 -2 +1 -2 2 4 -1 +2 -1 -2 +1 -2 +1 +2 2 8 +1 +2 +2 +1 +1 -2 +1 -2 -2 +1 -1 +1 -2 -2 +1 -1
Sample Output
1 1 0 1
Hint
#include<cstdio>
#include <algorithm>
#include<cstring>
using namespace std;
const int inf=1<<30;
const int nMax=5015;
const int mMax=10000000;
class edge{
public:
int v,nex;
};edge e[mMax];
int k,head[nMax];//head[i]是以點i爲起點的鏈表頭部
void addedge(int a,int b){//向圖中加邊的算法,注意加上的是有向邊//b爲a的後續節點既是a---->b
e[k].v=b;
e[k].nex=head[a];
head[a]=k;k++;
}
int dfn[nMax],low[nMax],sta[nMax],top,atype,belon[nMax],dep; //atype 強連通分量的個數
bool insta[nMax];
void Tarjan(int u){ //我的Tarjan模版
int i,j;
dfn[u]=low[u]=++dep;
sta[++top]=u;
insta[u]=1;
for(i=head[u];i;i=e[i].nex){
int v=e[i].v;
if(!dfn[v]){
Tarjan(v);
low[u]=min(low[u],low[v]);
}
else{
if(insta[v])low[u]=min(low[u],dfn[v]);
}
}
if(dfn[u]==low[u]){
atype++; //強連通分量個數
do{
j=sta[top--];
belon[j]=atype; //第j個點屬於第type個連通塊
insta[j]=0;
}while(u!=j);
}
}
int a[nMax]; //每個連通塊的出度
int b[nMax];
void init(){
k=1;
dep=1;
top=atype=0;
memset(insta,0,sizeof(insta)); //是否在棧中
memset(head,0,sizeof(head)); //靜態鏈表頭指針
memset(low,0,sizeof(low)); //Tarjan的low數組
memset(dfn,0,sizeof(dfn)); //Tarjan的dfn數組
memset(belon,0,sizeof(belon)); //記錄每個點屬於哪一個強連通分量
}
int n,m;
bool judge(){
for(int i=1;i<=n;i++){
if(belon[i*2]==belon[i*2+1]){
return 0;
}
}
return 1;
}
int main(){
int i,j,a1,a2,c1,c2;
char str1,str2;
while(scanf("%d%d",&n,&m)!=EOF){
init();
for(i=1;i<=m;i++){
c1=c2=1;
scanf("%d%d",&a1,&a2);
if(a1<0){
c1=0;
a1=-a1;
}
if(a2<0){
c2=0;
a2=-a2;
}
addedge(2*a1+c1,2*a2+1-c2);
addedge(2*a2+c2,2*a1+1-c1);
}
for(i=1;i<=2*n+1;i++){
if(!dfn[i])Tarjan(i);
}
if(judge())printf("1");
else printf("0");
printf("\n");
}
return 0;
}