poj2942(기이한 고리가 존재하는 연결 블록 구하기)
Time Limit: 7000MS
Memory Limit: 65536K
Total Submissions: 4896
Accepted: 1521
Description
Being a knight is a very attractive career: searching for the Holy Grail, saving damsels in distress, and drinking with the other knights are fun things to do. Therefore, it is not very surprising that in recent years the kingdom of King Arthur has experienced an unprecedented increase in the number of knights. There are so many knights now, that it is very rare that every Knight of the Round Table can come at the same time to Camelot and sit around the round table; usually only a small group of the knights isthere, while the rest are busy doing heroic deeds around the country.
Knights can easily get over-excited during discussions-especially after a couple of drinks. After some unfortunate accidents, King Arthur asked the famous wizard Merlin to make sure that in the future no fights break out between the knights. After studying the problem carefully, Merlin realized that the fights can only be prevented if the knights are seated according to the following two rules:
Merlin will let the knights sit down only if these two rules are satisfied, otherwise he cancels the meeting. (If only one knight shows up, then the meeting is canceled as well, as one person cannot sit around a table.) Merlin realized that this means that there can be knights who cannot be part of any seating arrangements that respect these rules, and these knights will never be able to sit at the Round Table (one such case is if a knight hates every other knight, but there are many other possible reasons). If a knight cannot sit at the Round Table, then he cannot be a member of the Knights of the Round Table and must be expelled from the order. These knights have to be transferred to a less-prestigious order, such as the Knights of the Square Table, the Knights of the Octagonal Table, or the Knights of the Banana-Shaped Table. To help Merlin, you have to write a program that will determine the number of knights that must be expelled.
Input
The input contains several blocks of test cases. Each case begins with a line containing two integers 1 ≤ n ≤ 1000 and 1 ≤ m ≤ 1000000 . The number n is the number of knights. The next m lines describe which knight hates which knight. Each of these m lines contains two integers k1 and k2 , which means that knight number k1 and knight number k2 hate each other (the numbers k1 and k2 are between 1 and n ).
The input is terminated by a block with n = m = 0 .
Output
For each test case you have to output a single integer on a separate line: the number of knights that have to be expelled.
Sample Input
5 5
1 4
1 5
2 5
3 4
4 5
0 0
Sample Output
2
Hint
Huge input file, 'scanf' recommended to avoid TLE.
Source
Central Europe 2005
제목:http://poj.org/problem?id=2942
분석: 이 문제는 문제를 보고 ac까지 하루 정도 걸렸다. 우선 비극적인 것은 변연통이라고 생각하고 나중에 쌍연통을 구할 때 틀렸다. 그래서 계속 wa를 했다. 그리고 지금은 한 군데 더 이해하지 못한다. 바로 절단점을 판단할 때 뿌리+다자수, 비뿌리+dfn[u]<=low[v]로 나누어 뜻밖에도 wa를 판단하고 직접
ffn[u]<=low[v]는 AC입니다. 정말 곤란합니다. 나중에 연결 블록의 가장자리를 모두 저장하고 직접 판단하려고 했습니다. 그러나 좌절되었을 수도 있습니다. 계속 wa, 몰라요. 먼저 이렇게...
누가 뿌리를 어떻게 판단하는지 알려줬어~~
코드:
#include<cstdio>
#include<cstring>
#include<vector>
#define min(a,b) (a<b?a:b)
using namespace std;
const int mm=2222222;
const int mn=1111;
vector <int > arr[mn];
int t[mm],p[mm];
int h[mn],dfn[mn],low[mn],q[mn];
bool get[mn][mn],col[mn],ok;
int i,j,k,n,m,top,idx,cnt,ans,rt;
void dfs(int u,int fa,bool c)
{
dfn[u]=low[u]=++idx;
col[u]=c;
for(int i=h[u],v; i>=0; i=p[i])
if(!dfn[v=t[i]])
{
q[top++]=i;
dfs(v,u,!c);
low[u]=min(low[u],low[v]);
if(dfn[u]<=low[v])
{
arr[++cnt].push_back(v);
arr[cnt].push_back(u);
while((v=q[--top])!=i)arr[cnt].push_back(t[v]);
}
}
else if(v!=fa)low[u]=min(low[u],dfn[v]);
}
void tarjan()
{
memset(dfn,0,sizeof(int)*(n+1));
top=cnt=idx=0;
for(int i=1; i<=n; ++i)
if(!dfn[i])
{
dfs(rt=i,0,0);
if(top>0)
{
arr[++cnt].push_back(i);
while(top--)arr[cnt].push_back(t[q[top]]);
}
}
}
int main()
{
while(scanf("%d%d",&n,&m),n+m)
{
for(i=1; i<=n; ++i)memset(get[i],1,sizeof(bool)*(n+1)),h[i]=-1,arr[i].clear();
while(m--)scanf("%d%d",&i,&j),get[i][j]=get[j][i]=0;
for(m=0,i=1; i<=n; ++i)
for(j=1; j<=n; ++j)
if(i!=j&&get[i][j])t[m]=j,p[m]=h[i],h[i]=m++;
tarjan();
memset(q,0,sizeof(int)*(n+1));
for(k=1; k<=cnt; ++k)
{
if(arr[k].size()<3)continue;
for(i=ok=0; i<arr[k].size()&&!ok; ++i)
for(j=i+1; j<arr[k].size()&&!ok; ++j)
if(get[arr[k][i]][arr[k][j]]&&col[arr[k][i]]==col[arr[k][j]])ok=1;
if(ok)for(i=0; i<arr[k].size(); ++i)q[arr[k][i]]=1;
}
for(ans=0,i=1; i<=n; ++i)
if(!q[i])++ans;
printf("%d
",ans);
}
return 0;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Vuetify에서 행 그룹화이 기사에서는 유사한 값으로 테이블의 행을 그룹화하는 방법에 대한 경험을 공유하고자 합니다. 물론 기본 그룹화 예제를 찾을 수 있지만 제 사용 사례에는 약간의 고급 기능이 필요했습니다. 제품 데이터가 있다고 가정합니...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.