堆与并查集。洛谷P3378&P3367

晚上A了一堆板子题
我太弱了!
堆:洛谷P3378
AC代码:(STL

// luogu-judger-enable-o2
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<map>
#include<set>
#include<queue>
#define INF 0x3f3f3f3f
#define inf 0x3f
#define ll long long
using namespace std;
priority_queue<int,vector<int>,greater<int> > q;
int a,n,sum,qwq;
int main(){
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a;
        if(a==1){
            cin>>sum;
            q.push(sum);
        }
        if(a==2){
            qwq=q.top();
            cout<<qwq<<endl; 
        }
        if(a==3) q.pop(); 
    }
    return 0;

开氧气跑得很快。
并查集:P3367
使用路径压缩。

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<map>
#include<set>
#include<queue>
#define INF 0x3f3f3f3f
#define inf 0x3f
#define ll long long
using namespace std;
int fa[10001],n,m,z,x,y;
inline void clear(){
    for(int i=1;i<=n;i++) fa[i]=i; 
}
int find(int x){
    if(fa[x]==x) return x;
    return fa[x]=find(fa[x]);
}
int merge(int x,int y){
    int xx=find(x);
    int yy=find(y);
    if(xx==yy) return 0;
    fa[xx]=yy;
    return 1;
}
int main(){
    cin>>n>>m;
    clear();

    for(int i=1;i<=m;i++){
        cin>>z>>x>>y;
        if(z==1) merge(x,y);
        if(z==2) {
            if(find(x)==find(y)) cout<<"Y"<<endl;
            else cout<<"N"<<endl;
        }
    }
    return 0;
}

 

posted @ 2018-08-08 21:31  kenlig  阅读(154)  评论(0编辑  收藏  举报