代码:
#include<bits/stdc++.h>
using namespace std;
const int N = 1e4+5;
int n,m;
int acc[N];
int find(int x)
{
if(x!=acc[x]) acc[x] = find(acc[x]);
return acc[x];
}
int main()
{
cin>>n>>m;
for(int i=1;i<=n;i++) acc[i] = i;
while(m--)
{
int op,a,b;
cin>>op>>a>>b;
if(op==1)
{
int t1 = find(a),t2 = find(b);
if(t1!=t2) acc[t1] = t2;
}
if(op==2)
{
int t1 = find(a),t2 = find(b);
if(t1==t2) cout<<"Y\n";
else cout<<"N\n";
}
}
return 0;
}
题意:
思考:
代码:
#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define PII pair<int,int >
#define int long long
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
const int N = 1e6+10,M = 2000;
int acc[N],cnt = 0;
string fa;
map<string,int> mp;
map<int,string> pm;
int find(int x)
{
if(x!=acc[x]) acc[x] = find(acc[x]);
return acc[x];
}
signed main()
{
IOS;
for(int i=1;i<=1e6;i++) acc[i] = i;
while(1)
{
char op;
string s;
cin>>op;
if(op=='$') break;
cin>>s;
if(mp[s]==0)
{
cnt++;
mp[s] = cnt;
pm[cnt] = s;
}
if(op=='#') fa = s;
if(op=='+')
{
int t1 = find(mp[s]),t2 = find(mp[fa]);
if(t1!=t2) acc[t1] = t2;
}
if(op=='?')
{
cout<<s<<" "<<pm[find(mp[s])]<<"\n";
}
}
return 0;
}
题意:
思考:
代码:
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e6+10;
int n,a,b,p;
int vis[N],pri[N],cnt;
int acc[N];
int find(int x)
{
if(x!=acc[x]) acc[x] = find(acc[x]);
return acc[x];
}
void init(int x)
{
for(int i=2;i<=x;i++)
{
if(vis[i]==0) pri[++cnt] = i;
for(int j=1;pri[j]*i<=x;j++)
{
vis[pri[j]*i] = 1;
if(i%pri[j]==0) break;
}
}
}
signed main()
{
init(1e5+5);
cin>>a>>b>>p;
for(int i=a;i<=b;i++) acc[i] = i;
for(int i=1;i<=cnt;i++)
{
if(pri[i]<p) continue;
vector<int > v;
for(int j=1;;j++)
{
int x = pri[i]*j;
if(x>b) break;
if(x>=a&&x<=b) v.push_back(x);
}
for(int j=1;j<v.size();j++)
{
int a1 = v[0],b1 = v[j];
int t1 = find(a1),t2 = find(b1);
if(t1!=t2) acc[t1] = t2;
}
}
int sum = 0;
for(int i=a;i<=b;i++) if(i==find(i)) sum++;
cout<<sum;
}