UOJ Logo

NOI.AC

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#201671#3508. 朋友OoXiao_QioO100235ms30168kbC++113.5kb2024-02-04 11:25:252024-02-04 12:07:22

answer

#include<bits/stdc++.h>

using namespace std;

#define endl '\n'
#define hh cout<<endl;
#define No cout<<"No"<<endl;
#define Yes cout<<"Yes"<<endl;
#define NO cout<<"NO"<<endl;
#define YES cout<<"YES"<<endl;

namespace Fread{const int SIZE=1<<21;char buf[SIZE],*S,*T;inline char getchar(){if(S==T){T=(S=buf)+fread(buf,1,SIZE,stdin);if(S==T)return'\n';}return*S++;}}namespace Fwrite{const int SIZE=1<<21;char buf[SIZE],*S=buf,*T=buf+SIZE;inline void flush(){fwrite(buf,1,S-buf,stdout);S=buf;}inline void putchar(char c){*S++=c;if(S==T)flush();}struct NTR{~NTR(){flush();}}ztr;}namespace fastIO1{struct Reader{template<typename T>Reader&operator>>(T&x){char c=getchar();T f=1;while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}x=0;while(c>='0'&&c<='9'){x=x*10+(c-'0');c=getchar();}x*=f;return*this;}Reader&operator>>(char&c){c=getchar();while(c==' '||c=='\n')c=getchar();return*this;}Reader&operator>>(char*str){int len=0;char c=getchar();while(c==' '||c=='\n')c=getchar();while(c!=' '&&c!='\n'&&c!='\r'){str[len++]=c;c=getchar();}str[len]='\0';return*this;}Reader(){}}fin;struct Writer{template<typename T>Writer&operator<<(T x){if(x==0){putchar('0');return*this;}if(x<0){putchar('-');x=-x;}static int sta[45];int top=0;while(x){sta[++top]=x%10;x/=10;}while(top){putchar(sta[top]+'0');--top;}return*this;}Writer&operator<<(char c){putchar(c);return*this;}Writer&operator<<(char*str){int cur=0;while(str[cur])putchar(str[cur++]);return*this;}Writer&operator<<(const char*str){int cur=0;while(str[cur])putchar(str[cur++]);return*this;}Writer(){}}fout;}using fastIO1::Reader;using fastIO1::Writer;using fastIO1::fin;using fastIO1::fout;
#define cin fin
#define cout fout
using namespace fastIO1;

namespace fastIO2{template<typename T>void read(T&x){x=0;char ch;cin>>ch;T fl=1;while(ch<'0'||ch>'9'){if(ch=='-')fl=-1;cin>>ch;};while(ch>='0'&&ch<='9'){x=x*10+ch-'0';cin>>ch;};x=x*fl;}template<typename T,typename...T1>void read(T&x,T1&...x1){read(x);read(x1...);}template<typename T>void print(T x){if(x<0){x=-x;cout<<'-';}if(x/10)print(x/10);cout<<x%10;}template<typename T>void printsp(T x){print(x);cout<<' ';}template<typename T,typename...T1>void printsp(T&x,T1&...x1){printsp(x);printsp(x1...);}template<typename T>void printen(T x){print(x);cout<<endl;}template<typename T,typename...T1>void printen(T&x,T1&...x1){printen(x);printen(x1...);}}
using namespace fastIO2;

typedef long long ll;

mt19937 rnd(time(0));

ll ksm(ll a,ll b)
{
    if(!b)
        return 1;
    ll ans = 1;
    while(b)
    {
        if(b&1)
            ans *= a;
        a *= a;
        b /= 2;
    }
    return ans;
}
int n;
int a[1000001],b[1000001];
int f[1000001];
int find(int x)
{
    if(x!=f[x])
        f[x] = find(f[x]);
    return f[x];
}
bool merge(int x,int y)
{
    int xx = find(x);
    int yy = find(y);
    if(xx==yy)
        return 0;
    f[xx] = yy;
    return 1;
}
vector<int> m[1000001];
int c[1000001];
void solve()
{
	int i,j;
    cin>>n;
    for(i=1;i<=n;i++)
        cin>>a[i]>>b[i],f[i] = i;
    for(i=1;i<=n;i++)
    {
        m[a[i]].push_back(i);
        if(a[i]!=b[i])
            m[b[i]].push_back(i);
    }
    for(auto x:m)
    {
        // if(!m[i].size())
            // continue;
        for(j=1;j<x.size();j++)
            merge(x[0],x[j]);
    }
    int cnt = 0;
    for(i=1;i<=n;i++)
        c[find(i)]++;
    for(i=1;i<=n;i++)
        cnt += c[i]/2;
    cout<<cnt<<endl;
    return;
}
signed main()
{
    int T = 1;
    // cin>>T;
    while(T--)
        solve();
    return 0;
}

详细

小提示:点击横条可展开更详细的信息

Test #1:

score: 10
Accepted
time: 3ms
memory: 24648kb

input:

10
19 12
8 18
6 15
4 18
15 3
8 2
6 18
18 7
18 3
8 3

output:

4

result:

ok single line: '4'

Test #2:

score: 10
Accepted
time: 4ms
memory: 24648kb

input:

10
19 16
14 8
8 12
7 10
15 5
7 10
17 7
5 4
9 6
6 12

output:

4

result:

ok single line: '4'

Test #3:

score: 10
Accepted
time: 11ms
memory: 24644kb

input:

10
7 17
11 9
18 5
11 14
10 11
7 18
11 9
4 1
3 16
8 17

output:

4

result:

ok single line: '4'

Test #4:

score: 10
Accepted
time: 12ms
memory: 24644kb

input:

20
11 1
29 27
11 34
10 27
9 10
10 21
17 18
19 14
26 14
6 26
38 30
40 11
13 7
33 9
10 32
40 28
20 21
...

output:

7

result:

ok single line: '7'

Test #5:

score: 10
Accepted
time: 10ms
memory: 24648kb

input:

20
19 21
15 9
33 10
9 15
5 32
10 21
28 19
22 11
20 4
32 10
26 8
13 27
30 7
11 22
2 35
8 14
17 28
9 3...

output:

7

result:

ok single line: '7'

Test #6:

score: 10
Accepted
time: 4ms
memory: 24648kb

input:

20
19 10
8 14
6 27
24 19
40 38
17 29
30 28
33 8
23 26
10 11
7 34
2 26
35 3
3 28
20 17
35 30
30 36
18...

output:

7

result:

ok single line: '7'

Test #7:

score: 10
Accepted
time: 49ms
memory: 30156kb

input:

100000
92387 35422
124898 132532
192988 84636
99872 57831
131700 147597
179017 125316
96560 4822
318...

output:

40939

result:

ok single line: '40939'

Test #8:

score: 10
Accepted
time: 48ms
memory: 30160kb

input:

100000
8515 151563
105451 194713
109537 130709
63343 41819
65855 51779
39457 85060
96650 174359
1936...

output:

40841

result:

ok single line: '40841'

Test #9:

score: 10
Accepted
time: 47ms
memory: 30160kb

input:

100000
91939 100407
110197 24191
58791 9486
68030 25807
11 188665
132600 12100
129445 33496
196658 1...

output:

40820

result:

ok single line: '40820'

Test #10:

score: 10
Accepted
time: 47ms
memory: 30168kb

input:

100000
30518 196518
74071 159971
150121 4862
43967 173607
19138 90754
19513 171337
183499 149873
142...

output:

40816

result:

ok single line: '40816'

Extra Test:

score: 0
Extra Test Passed