B. Wang's worries (combined investigation)

Description
As we all know, King Arthur has 13 Knights of the Round Table. However, after Fate/Zero, the king heard the advice of Knight Atlanta and decided to change his rule of the Knights.
After a night of contemplation, Wang thought of N kinds of ruling strategies, and she decided to implement as many of them as possible.
But at the same time, she discovered another problem: some of these strategies are similar in content. If these strategies are executed at the same time, it will cause the knight to resent. She decided to strictly avoid this situation.
At the same time, the content of these strategies is similar and transitive. If strategy A and strategy B are similar, strategy B and strategy C are similar, then we think strategy A and strategy C are also similar.

The
first line of Input , two integers N and M; N represents the number of strategies that Knight King Arthur came up with (13≤N≤1000000).
Lines 2 to M+1 contain two integers in each line, indicating that the two strategies are similar.


One integer in one line of Output indicates how many strategies King Arthur can choose at most.

Samples
Input Copy
5 4
1 3
1 3
2 3
4 5
Output
2
Hint
[Data Range]

For 30% of the data, 13≤N, M≤100.
For 100% data, 13≤N, M≤1000000.

And check the collection, there’s nothing to say. I’m over. I really have nothing to say.
But when I look at the data, I have reached 1e6. I’m over.
Really, I fired a shot in a second. What can you say?

When I first started using cin cout, I saw it
timed out and I was surprised. How could the timeout be changed? Then I shot the dragon and changed the sacnf
printf directly. It was a big shot. I shot it for a second. What can you say?

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
priority_queue <int,vector<int>,less<int> > q;
priority_queue <int,vector<int>,greater<int> > pp;
vector <int> aa;

const int maxn=1e6+888;
const int N=1e6+1888;
int p[maxn];
int b[maxn];
ll sum=0,bn;
char s[1000007];
char c[maxn];
ll n,m;  int jin=0,w=1;
int cnpo(int x)  //此处是俄语的询问 哈哈哈
{
    
    
    if(p[x]==x)
        return x;
    return p[x]=cnpo(p[x]);
}
void join(int x,int y)
{
    
    
    int px=cnpo(x);
    int py=cnpo(y);

    if(px==py)
        return;
    if(px!=py)
        p[px]=py;
}
int main ()
{
    
    

   scanf("%lld%lld",&n,&m);
    for(int i=1;i<=n;i++)
    {
    
    
        p[i]=i;
    }
    for(int i=0;i<m;i++)
    {
    
    
        int x,y; scanf("%d%d",&x,&y);
        join(x,y);
    }
    for(int i=1;i<=n;i++)
    {
    
    
        if(p[i]==i)
            sum++;
    }
   printf("%lld",sum);
    return 0;
}

Guess you like

Origin blog.csdn.net/qq_52172364/article/details/113142464