zoukankan      html  css  js  c++  java
  • hdu 4496 其实还是并查集

    
    
    Problem Description
    
    
    Luxer is a really bad guy. He destroys everything he met. 
    One day Luxer went to D-city. D-city has N D-points and M D-lines. Each D-line connects exactly two D-points. Luxer will destroy all the D-lines. The mayor of D-city wants to know how many connected blocks of D-city left after Luxer destroying the first K D-lines in the input. 
    Two points are in the same connected blocks if and only if they connect to each other directly or indirectly.
    
    
     
    
    
    Input
    
    
    First line of the input contains two integers N and M. 
    Then following M lines each containing 2 space-separated integers u and v, which denotes an D-line. 
    Constraints: 
    0 < N <= 10000 
    0 < M <= 100000 
    0 <= u, v < N. 
    
    
     
    
    
    Output
    
    
    Output M lines, the ith line is the answer after deleting the first i edges in the input.
    
    
     逆向思维的重要性!

    #include<cstdio> #include<iostream> #include<string.h> #include<stack> #define maxn 10005
    using namespace std; int pre[maxn]; int num; void init() { for(int i=0;i<maxn;i++) pre[i]=i; } int find(int x) { if(pre[x]==x) return x; else return pre[x]=find(pre[x]); } void merge(int x,int y) { x=find(x); y=find(y); if(x!=y) { num--; pre[y]=x; } } int main() { cin.sync_with_stdio(false); int n,m; while(cin>>n>>m) { stack<int> fuck,fuck2; int ret[m]; while(m--) { int x,y; cin>>x>>y; fuck.push(x),fuck.push(y); } init(); num=n; int rett=0; while(!fuck.empty()) { int yy=fuck.top(); fuck.pop(); int xx=fuck.top(); fuck.pop(); ret[rett++]=num; merge(xx,yy); } for(int i=rett-1;i>=0;i--) cout<<ret[i]<<endl; } return 0; }
  • 相关阅读:
    反编译Silverlight项目
    李开复关于创新的五条建议
    Android人脸检测类FaceDetector
    吃火锅如何进行口腔保健
    ListView优化
    番茄工作法
    App Inventor for Android初接触
    QPainter如何自适应大小画图
    Eclipsejava.lang.OutOfMemoryError: PermGen space
    健康小工具——体脂肪率自测
  • 原文地址:https://www.cnblogs.com/z1141000271/p/5791194.html
Copyright © 2011-2022 走看看