跳到正文

个人记录

总结 (代码)

wuweize150815约 7 分钟
原文

这是 2026年5月23日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

收录
2026年5月23日
更新
2026年5月23日
篇幅
约 7 分钟

版本

  1. v2当前
  2. v1

第一题:

C++
#include <bits/stdc++.h>
using namespace std;
int fa[100005];
int n , m , x , y , cnt=0;
int find (int x)
{
	if (fa[x]==x) return x;
	return fa[x]=find (fa[x]);
}
int main ( )
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin >> n >> m;
	cnt=n;
	for (int i=1;i<=n;i++)
	{
		fa[i]=i;
	}
	while (m--)
	{
		cin >> x >> y;
		int nx=find (x) , ny=find (y);
		if (nx==ny) continue;
		cnt--;
		fa[ny]=nx;
	}
	cout << cnt;
	return 0;
} 

第二题:

C++
#include <bits/stdc++.h>
using namespace std;
map <string , string> fa;
string s , x , y;
int n , m , k;
string find (string x)
{
	if (fa[x]==x) return x;
	return fa[x]=find (fa[x]);
 } 
int main ( )
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin >> n >> m;
	for (int i=1;i<=n;i++)
	{
		cin >> s;
		fa[s]=s;
	}
	while (m--)
	{
		cin >> x >> y;
		string nx=find (x) , ny=find (y);
		if (nx==ny) continue;
		fa[ny]=nx;
	}
	cin >> k;
	while (k--)
	{
		cin >> x >> y;
		string nx=find (x) , ny=find (y);
		if (nx==ny)
		{
			cout << "Yes.\n";
		}
		else
		{
			cout << "No.\n";
		}
	}
	return 0;
} 

第三题:

C++
#include <bits/stdc++.h>
using namespace std;
struct node
{
	int x , y;
};
const int dx[4]={0 , 0 , 1 , -1} , dy[4]={1 , -1 , 0 , 0};
int v[505][505];
int x[250005] , y[250005];
bool vis[505][505];
int n , m , cnt=0 , mid;
bool k;
void bfs (int x , int y)
{
	memset (vis , 0 , sizeof (vis));
	queue <node> q;
	q.push ({x , y});
	while (!(q.empty ( )))
	{
		node p=q.front ( );
		q.pop ( );
		if (vis[p.x][p.y]==1) continue;
		vis[p.x][p.y]=1;
		for (int i=0;i<=3;i++)
		{
			int nx=p.x+dx[i] , ny=p.y+dy[i];
			if (nx<1||nx>n||ny<1||ny>m) continue;
			if (abs (v[nx][ny]-v[p.x][p.y])>mid) continue;
			q.push ({nx , ny});
		}
	}
}
int main ( )
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin >> n >> m;
	for (int i=1;i<=n;i++)
	{
		for (int j=1;j<=m;j++)
		{
			cin >> v[i][j];
		}
	}
	for (int i=1;i<=n;i++)
	{
		for (int j=1;j<=m;j++)
		{
			cin >> k;
			if (k==1)
			{
				cnt++;
				x[cnt]=i , y[cnt]=j;
			}
		}
	}
	int l=0 , r=1e9;
	while (l<r)
	{
        mid=(l+r)/2;
        bfs (x[1] , y[1]);
        bool p=0;
        for (int i=1;i<=cnt;i++)
        {
            if (vis[x[i]][y[i]]==0)
            {
                p=1;
                break;
            }
        }
		if (p==0)
		{
			r=mid;
		}
		else
		{
			l=mid+1;
		}
	}
	cout << l << "\n";
	return 0;
} 

第四题:

C++
#include <bits/stdc++.h>
using namespace std;
map <int , bool> mp;
int fa[100005];
int a , b , p;
int find (int x)
{
	if (fa[x]==x) return x;
	return fa[x]=find (fa[x]);
}
bool zs (int x)
{
	if (x<=1) return 0;
	for (int i=2;i<=sqrt (x);i++)
	{
		if (x%i==0) return 0;
	}
	return 1;
}
int main ( )
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin >> a >> b >> p;
	for (int i=1;i<=b;i++)
	{
		fa[i]=i;
	}
	for (int i=p;i<=b;i++)
	{
		if (zs (i)==0) continue;
		for (int j=i;j<=b;j+=i)
		{
			int nx=find (i) , ny=find (j);
			if (nx==ny) continue;
			fa[ny]=nx;
		}
	}
	for (int i=a;i<=b;i++)
	{
		int p=find (i);
		mp[p]=1;
	}
	cout << mp.size ( );
	return 0;
} 

第五题:

C++
#include <bits/stdc++.h>
using namespace std;
int vis[1000005] , fa[1000005];
int n , m , p , q;
int find (int x)
{
    if (fa[x]==x) return x;
    return fa[x]=find (fa[x]);
}
int main ( )
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin >> n >> m >> p >> q;
    for (int i=1;i<=n+1;i++)
    {
        fa[i]=i;
    }
	for (int i=m;i>=1;i--)
	{
		int x=(i*p+q)%n+1 , y=(i*q+p)%n+1;
        if (x>y) swap (x , y);
		for (int j=find (x);j<=y;j=find (j+1))
		{
			fa[j]=j+1;
            vis[j]=i;
		}
	}
	for (int i=1;i<=n;i++)
	{
		cout << vis[i] << "\n";
	}
	return 0;
}