11.22

· · 个人记录

练习

::::::success[T1] :::::info[题目链接] T696602 亲戚 :::::

:::::success[正确代码]

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int p[5005];
int find(int x){
    if(p[x]==x) return x;
    return p[x]=find(p[x]);
}
void merge(int a,int b){
    int t1=find(a),t2=find(b);
    if(t1!=t2) p[t2]=t1;
    return;
}
int main() {
    ios::sync_with_stdio(0);
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n,m,k;
    cin>>n>>m>>k;
    for(int i=1;i<=n;i++) p[i]=i;
    for(int i=1;i<=m;i++){
        int x,y;
        cin>>x>>y;
        merge(x,y);
    }
    for(int i=1;i<=k;i++){
        int k1,k2;
        cin>>k1>>k2;
        if(find(k1)==find(k2)) cout<<"Yes\n";
        else cout<<"No\n";
    }
    return 0;
}
:::::

::::::success[T2] :::::info[题目链接] T698408 分组查询 :::::

:::::success[正确代码]

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
map<string,string> p;
string s1;
string find(string x){
    if(p[x]==x) return x;
    return p[x]=find(p[x]);
}
void merge(string a,string b){
    string t1=find(a),t2=find(b);
    if(t1!=t2) p[t2]=t1;
    return;
}
int main() {
    ios::sync_with_stdio(0);
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n,m,k;
    cin>>n>>m;
    for(int i=1;i<=n;i++) cin>>s1,p[s1]=s1;
    for(int i=1;i<=m;i++){
        string x,y;
        cin>>x>>y;
        merge(x,y);
    }
    cin>>k;
    for(int i=1;i<=k;i++){
        string k1,k2;
        cin>>k1>>k2;
        if(find(k1)==find(k2)) cout<<"Yes.\n";
        else cout<<"No.\n";
    }
    return 0;
}
:::::

::::::success[T3] :::::info[题目链接] T698085 游戏套装选购 :::::

:::::success[正确代码]

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=200005;
int n, m, w, c[N], d[N], p[N];
int dp[N];
int find(int x){
    if (p[x] == x) return x;
    return p[x] = find(p[x]);
}
void merge(int a, int b) {
    int t1 = find(a);
    int t2 = find(b);
    if (t1 != t2) p[t2] = t1, c[t1] += c[t2], d[t1] += d[t2];
}
int main() {
    ios::sync_with_stdio(0);
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> m >> w;
    for(int i = 1; i <= n; i++){
        cin >> c[i] >> d[i];
        p[i] = i;
    }
    for(int i = 1, a, b; i <= m; i++){
        cin >> a >> b;
        merge(a, b);
    }
    int nn = 0;
    for(int i = 1; i <= n; i++)
        if (p[i] == i) {
            c[++nn] = c[i];
            d[nn] = d[i];
        }
    for(int i = 1; i <= nn; i++)
        for(int j = w; j >= c[i]; j--)
            dp[j] = max(dp[j], dp[j - c[i]] + d[i]);
    cout << dp[w];
    return 0;
}
:::::

::::::success[T4] :::::info[题目链接] T696738 数字小队 ::::: :::::success[正确代码]

#include<bits/stdc++.h>
using namespace std;

const int N = 100005;
int p[N], vis[N];

int find(int x) {
    if (p[x] != x) p[x] = find(p[x]);
    return p[x];
}

void merge(int a, int b) {
    int pa = find(a), pb = find(b);
    if (pa != pb) p[pb] = pa;
}

int main() {
    int a, b, k;
    cin >> a >> b >> k;
    for (int i = a; i <= b; i++)
    {
        p[i] = i;
    }
    vis[1] = 1; 
    for (int i = 2; i <= b; i++) {
        if (vis[i] == 0) 
        {
            if (i < k) 
            {
                for(int j=i*2; j<=b; j+=i) vis[j] = 1;
            }
            else 
            {
                int x = -1;
                if(i >= a && i <= b) x = i; 
                for(int j = i*2; j<=b; j+=i)
                {
                    vis[j] = 1;
                    if(j >= a)
                    {
                        if (x == -1) x = j;
                        else merge(x, j);
                    }
                }
            }
        }
    }

    int ans = 0;
    for (int i = a; i <= b; i++) {
        ans += (p[i] == i);
    }
    cout << ans;
    return 0;
}
:::::

::::::success[T5] :::::info[题目链接] [P12175 T699580 外星人入侵2 ::::: :::::success[正确代码]

方法1:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=2e5;
int n,m,p[N],sz[N];
int find(int x){
    if(p[x]==x) return x;
    return p[x]=find(p[x]);
}
void merge(int a, int b) {
    int t1=find(a);
    int t2=find(b);
    if(t1!=t2)p[t2]=t1,sz[t1]+=sz[t2];
}
int main() {
    ios::sync_with_stdio(0);
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin>>n>>m;
    for(int i=1;i<=n;i++) p[i]=i,sz[i]=1;
    for(int i=1,x,y;i<=m;i++){
        cin>>x>>y;
        merge(x,y);
    }
    int ans=0;
    for(int i=1;i<=n;i++)
        ans=max(ans,sz[i]);
    cout<<ans;
    return 0;
}
:::::