[NOI2002] 은하수 영웅전설(및 수집)
9346 단어 문제풀이함께 조사하여 모으다NOI
제목 설명
전송문
문제풀이의 방향
before는 i 앞에 몇 개의 원소가 있음을 표시하고,count[i]는 i가 있는 몇 개의 원소를 표시하며 수집한다.
코드 #include
#include
#include
#include
using namespace std;
int father[30005],before[30005],count[30005];
int n,i,x,y;
char z;
int find(int x){
int f;
if (father[x]==x) return father[x];
f=find(father[x]);
before[x]+=before[father[x]];
father[x]=f;
return father[x];
}
void merge(int x,int y){
int i=find(x);
int j=find(y);
father[i]=j;
before[i]+=count[j];
count[j]+=count[i];
return;
}
int main(){
scanf("%d",&n);
for (i=1;i<=30001;++i){
father[i]=i;
count[i]=1;
before[i]=0;
}
for (i=1;i<=n;++i){
cin>>z;
cin>>x>>y;
if (z=='M')
merge(x,y);
if (z=='C')
if (find(x)!=find(y))
printf("-1
");
else
cout<<abs(before[x]-before[y])-1<<endl;
}
return 0;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
49일차 - 2022.04.20
Baekjoon에서 문제풀이
1) 문제 : 첫째 줄에는 별 1개, 둘째 줄에는 별 2개, N번째 줄에는 별 N개를 찍는 문제/ 첫째 줄에 N(1 ≤ N ≤ 100)이 주어진다.
첫째 줄부터 N번째 줄까지 차례대로 별...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.
before는 i 앞에 몇 개의 원소가 있음을 표시하고,count[i]는 i가 있는 몇 개의 원소를 표시하며 수집한다.
코드 #include
#include
#include
#include
using namespace std;
int father[30005],before[30005],count[30005];
int n,i,x,y;
char z;
int find(int x){
int f;
if (father[x]==x) return father[x];
f=find(father[x]);
before[x]+=before[father[x]];
father[x]=f;
return father[x];
}
void merge(int x,int y){
int i=find(x);
int j=find(y);
father[i]=j;
before[i]+=count[j];
count[j]+=count[i];
return;
}
int main(){
scanf("%d",&n);
for (i=1;i<=30001;++i){
father[i]=i;
count[i]=1;
before[i]=0;
}
for (i=1;i<=n;++i){
cin>>z;
cin>>x>>y;
if (z=='M')
merge(x,y);
if (z=='C')
if (find(x)!=find(y))
printf("-1
");
else
cout<<abs(before[x]-before[y])-1<<endl;
}
return 0;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
49일차 - 2022.04.20
Baekjoon에서 문제풀이
1) 문제 : 첫째 줄에는 별 1개, 둘째 줄에는 별 2개, N번째 줄에는 별 N개를 찍는 문제/ 첫째 줄에 N(1 ≤ N ≤ 100)이 주어진다.
첫째 줄부터 N번째 줄까지 차례대로 별...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.
#include
#include
#include
#include
using namespace std;
int father[30005],before[30005],count[30005];
int n,i,x,y;
char z;
int find(int x){
int f;
if (father[x]==x) return father[x];
f=find(father[x]);
before[x]+=before[father[x]];
father[x]=f;
return father[x];
}
void merge(int x,int y){
int i=find(x);
int j=find(y);
father[i]=j;
before[i]+=count[j];
count[j]+=count[i];
return;
}
int main(){
scanf("%d",&n);
for (i=1;i<=30001;++i){
father[i]=i;
count[i]=1;
before[i]=0;
}
for (i=1;i<=n;++i){
cin>>z;
cin>>x>>y;
if (z=='M')
merge(x,y);
if (z=='C')
if (find(x)!=find(y))
printf("-1
");
else
cout<<abs(before[x]-before[y])-1<<endl;
}
return 0;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
49일차 - 2022.04.20Baekjoon에서 문제풀이 1) 문제 : 첫째 줄에는 별 1개, 둘째 줄에는 별 2개, N번째 줄에는 별 N개를 찍는 문제/ 첫째 줄에 N(1 ≤ N ≤ 100)이 주어진다. 첫째 줄부터 N번째 줄까지 차례대로 별...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.