poj 2723 Get Luffy Out(2-sat 구도제)
Time Limit: 2000MS
Memory Limit: 65536K
Total Submissions: 6191
Accepted: 2327
Description
Ratish is a young man who always dreams of being a hero. One day his friend Luffy was caught by Pirate Arlong. Ratish set off at once to Arlong's island. When he got there, he found the secret place where his friend was kept, but he could not go straight in. He saw a large door in front of him and two locks in the door. Beside the large door, he found a strange rock, on which there were some odd words. The sentences were encrypted. But that was easy for Ratish, an amateur cryptographer. After decrypting all the sentences, Ratish knew the following facts:
Behind the large door, there is a nesting prison, which consists of M floors. Each floor except the deepest one has a door leading to the next floor, and there are two locks in each of these doors. Ratish can pass through a door if he opens either of the two locks in it. There are 2N different types of locks in all. The same type of locks may appear in different doors, and a door may have two locks of the same type. There is only one key that can unlock one type of lock, so there are 2N keys for all the 2N types of locks. These 2N keys were divided into N pairs, and once one key in a pair is used, the other key will disappear and never show up again.
Later, Ratish found N pairs of keys under the rock and a piece of paper recording exactly what kinds of locks are in the M doors. But Ratish doesn't know which floor Luffy is held, so he has to open as many doors as possible. Can you help him to choose N keys to open the maximum number of doors?
Input
There are several test cases. Every test case starts with a line containing two positive integers N (1 <= N <= 2
10) and M (1 <= M <= 2
11) separated by a space, the first integer represents the number of types of keys and the second integer represents the number of doors. The 2N keys are numbered 0, 1, 2, ..., 2N - 1. Each of the following N lines contains two different integers, which are the numbers of two keys in a pair. After that, each of the following M lines contains two integers, which are the numbers of two keys corresponding to the two locks in a door. You should note that the doors are given in the same order that Ratish will meet. A test case with N = M = 0 ends the input, and should not be processed.
Output
For each test case, output one line containing an integer, which is the maximum number of doors Ratish can open.
Sample Input
3 6
0 3
1 2
4 5
0 1
0 2
4 1
4 2
3 5
2 2
0 0
Sample Output 4
Source Beijing 2005
제목:http://poj.org/problem?id=2723
제목: n쌍의 열쇠를 드리겠습니다. 한 쌍은 하나만 골라서 사용할 수 있습니다. m문이 있고, 한 짝의 문은 두 개의 열쇠가 모두 열 수 있습니다. 열쇠를 어떻게 선택하느냐고 물어보세요. 문을 가장 많이 열고, 문은 순서대로 열어야 합니다.
분석: 처음에 열쇠 간의 대응 관계가 생각나지 않아서 2-sat로 그림을 만들 수 없었다. 나중에 잠시 생각해 보니 같은 문으로 두 개의 열쇠에 대응하는 대 열쇠(즉 같은 조의 다른 열쇠)를 선택할 수 없었다. 지금은 열쇠에 다른 번호를 주고 이 사고방식에 따라 그림을 만들면 된다...m가 비교적 크기 때문에 모든 2분 m가 타당성을 판단하면 된다
코드:
#include<cstdio>
#include<iostream>
using namespace std;
const int mm=4444;
const int mn=2222;
int ver[mm],next[mm];
int head[mn],dfn[mn],low[mn],q[mn],id[mn],x[mn],a[mn],b[mn];
int i,n,m,idx,cnt,top,edge,l,r,ans;
void add(int u,int v)
{
ver[edge]=v,next[edge]=head[u],head[u]=edge++;
}
void dfs(int u)
{
dfn[u]=low[u]=++idx;
q[top++]=u;
for(int i=head[u],v;i>=0;i=next[i])
if(!dfn[v=ver[i]])
dfs(v),low[u]=min(low[u],low[v]);
else if(!id[v])low[u]=min(low[u],dfn[v]);
if(low[u]==dfn[u])
{
id[u]=++cnt;
while(q[--top]!=u)id[q[top]]=cnt;
}
}
void Tarjan()
{
for(idx=cnt=top=i=0;i<n+n;++i)dfn[i]=id[i]=0;
for(i=0;i<n+n;++i)
if(!dfn[i])dfs(i);
}
bool ok()
{
for(edge=i=0;i<n+n;++i)head[i]=-1;
for(i=0;i<m;++i)
{
add(x[a[i]],x[b[i]]^1);
add(x[b[i]],x[a[i]]^1);
}
Tarjan();
for(i=0;i<n+n;i+=2)
if(id[i]==id[i^1])return 0;
return 1;
}
int main()
{
while(scanf("%d%d",&n,&m),n+m)
{
for(i=0;i<n;++i)
{
scanf("%d%d",&l,&r);
x[l]=i<<1|1,x[r]=i<<1;
}
for(i=0;i<m;++i)
scanf("%d%d",&a[i],&b[i]);
ans=l=0,r=m;
while(l<=r)
{
m=(l+r)>>1;
if(ok())ans=m,l=m+1;
else r=m-1;
}
printf("%d
",ans);
}
return 0;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
cocos2d Lua 학습(一)ios에서 루아 함수 호출 및 전참 방법 lua 코드: 출력 결과: lua 호출 C++ 방법: add 함수: lua 코드: 출력 결과: 함수를 호출합니다. 함수를 호출하려면 다음 협의를 따르십시오. 우선, 호출할 함...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.