题目总结:
蓝桥杯历届试题 合根植物
蓝桥杯历届试题 国王的烦恼
POJ2236
HDU1272
食物链
正题:
蓝桥杯历届试题 合根植物
问题描述
w星球的一个种植园,被分成 m * n 个小格子(东西方向m行,南北方向n列)。每个格子里种了一株合根植物。
这种植物有个特点,它的根可能会沿着南北或东西方向伸展,从而与另一个格子的植物合成为一体。
如果我们告诉你哪些小格子间出现了连根现象,你能说出这个园中一共有多少株合根植物吗?
输入格式
第一行,两个整数m,n,用空格分开,表示格子的行数、列数(1<m,n<1000)。
接下来一行,一个整数k,表示下面还有k行数据(0<k<100000)
接下来k行,第行两个整数a,b,表示编号为a的小格子和编号为b的小格子合根了。
格子的编号一行一行,从上到下,从左到右编号。
比如:5 * 4 的小格子,编号:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
17 18 19 20
样例输入
5 4
16
2 3
1 5
5 9
4 8
7 8
9 10
10 11
11 12
10 14
12 16
14 18
17 18
15 19
19 20
9 13
13 17
样例输出
5
#include<iostream>
#include<cstring>
#include<set>
using namespace std;
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
int pre[1000005];
void init(int a)
{
for(int i = 1 ; i <= a ; i++ ){
pre[i] = i;
}
}
int find(int a)
{
while( pre[a] != a) {
a = pre[a];
}
return a;
}
void join(int a,int b)
{
int x = find(a);
int y = find(b);
if(x == y) return ;
pre[x] = y;
}
int main()
{ IOS;
set<int> c;
int n,m,i,k,a,b;
cin>>m>>n;
cin>>k;
init(m*n);
for(i = 1;i <= k;i++){
cin>>a>>b;
join(a,b);
}
for(i = 1;i <= m*n;i++){
if(c.find(find(i)) == c.end()) c.insert(find(i));
}
cout<<c.size()<<endl;
return 0;
}
#include<iostream>
#include<cstring>
#include<algorithm>
#include<set>
using namespace std;
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
int pre[100005];
struct mmp
{
int x,y,t;
mmp(){}
mmp(int a,int b,int c)
{
x = a;y = b;t = c;
}
}ak[100005];
void init(int a)
{
for(int i = 1;i <= a; i++){
pre[i] = i;
}
}
int find(int a)
{
while(pre[a] != a){
a = pre[a];
}
return a;
}
bool cmp(mmp a,mmp b)
{
return a.t>b.t;
}
bool join(int a,int b)
{
int x = find(a);
int y = find(b);
if(x == y ) return false;
pre[x] = y;
return true;
}
int main()
{ IOS;
int n,m,i,a,b,t;
cin>>n>>m;
init(n);
for(i = 0;i < m; i++){
cin>>a>>b>>t;
ak[i]=mmp(a,b,t);
}
sort(ak,ak+m,cmp);
int ans=0,day = 0;
for(i = 0;i < m;i++){
if(join(ak[i].x,ak[i].y) && day != ak[i].t ) {
ans++;
day = ak[i].t;
}
}
cout<<ans<<endl;
return 0;
}
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#include<stack>
#include<cmath>
#include<cstdlib>
#include<map>
#include<set>
using namespace std;
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define inf 0x3f3f3f3f
#define ll long long
struct mmp
{
int pre,ranks,x,y;
}ave[1005];
int find(int k)
{
while(k != ave[k].pre) k = ave[k].pre;
return k;
}
double lo(mmp a,mmp b)
{
double ans = (a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y);
ans = sqrt(ans);
return ans;
}
int main()
{ IOS;
int n,d,dis[1005]; //用set容易超时
cin>>n>>d;
for(int i = 1;i <= n;i++){
cin>>ave[i].x>>ave[i].y;
ave[i].pre = i;
ave[i].ranks = 1;
}
char op;
int p,q;
memset(dis,0,sizeof(dis));
while(cin>>op){
if(op == 'O'){
cin>>p;
for(int i = 1;i <= n;i++){
if(dis[i]){
if(lo(ave[i],ave[p]) <= d){
int x = find(i);
int y = find(p);
if(x != y) {
if(ave[x].ranks < ave[y].ranks ) ave[x].pre = y;
else {
if(ave[x].ranks == ave[y].ranks) ave[x].ranks++;
ave[y].pre = x;
}
}
}
}
}
dis[p] = 1;
}
else {
cin>>p>>q;
if(find(p) == find(q)) cout<<"SUCCESS"<<endl;
else cout<<"FAIL"<<endl;
}
}
return 0;
}
这题也可以不用并查集,直接判断边的个数是不是点的个数减一。
#include<bits/stdc++.h>
using namespace std;
int a,b;
int main()
{
while(~scanf("%d%d",&a,&b)&&a!=-1&&b!=-1)
{
set<int> s;
s.insert(a);
s.insert(b);
if(a==0&&b==0)
{
cout<<"Yes"<<endl;
continue;
}
int sum=1;
while(1)
{
scanf("%d%d",&a,&b);
if(a==0&&b==0)break;
s.insert(a);
s.insert(b);
sum++;
}
if(s.size()==sum+1)cout<<"Yes"<<endl;
else cout<<"No"<<endl;
s.clear();
}
return 0;
}
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#include<stack>
#include<cmath>
#include<cstdlib>
#include<cstdio>
#include<map>
#include<set>
using namespace std;
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define inf 0x3f3f3f3f
#define ll long long
int read(){
int a=0,b=1;
char ch=getchar();
while((ch<'0'||ch>'9')&&(ch!='-')){
ch=getchar();
}
if(ch=='-'){
b=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9'){
a=a*10+ch-'0';
ch=getchar();
}
return a*b;
}
int n,k;
int ave[500005];
int find(int pre)
{
if(pre == ave[pre]) return pre;
return ave[pre] = find(ave[pre]);
}
void unit(int x,int y)
{
int a = find(x);
int b = find(y);
if(a != b) ave[a] = b;
}
void read(int &a,int &b)
{
a = read(); b = read();
return ;
}
void read(int &a,int &b,int &c)
{
a = read(); b = read(); c = read();
return ;
}
int main()
{ read(n,k);
for(int i = 1;i <= 3*n;i++) ave[i] = i;
int num = 0;
for(int i = 0;i < k;i++){
int d,c,v;
read(d,c,v);
if(c > n||v > n) {num++;continue;}
if(d == 1){
if(find(c) == find(v + n) || find(c) == find(v + 2*n)) {
num++;continue;
}
unit(c,v); unit(c + n,v + n); unit(c + 2*n,v + 2*n);
}
else {
if(c == v){num++;continue;}
if(find(c) == find(v) || find(c) == find(v + 2*n)){
num++;continue;
}
unit(c,v + n); unit(c + n,v + 2*n); unit(c + 2*n,v);
}
}
cout<<num<<endl;
return 0;
}