跳到正文

个人记录

智码 OI 提高组做题笔记

原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 9 分钟

版本

  1. v1当前

淼淼秒,淼淼妙!

水题场,我感觉难度最多黄蓝蓝黄,入门组的题吧。

A

哇这么板,前几天 U 刚讲过,还是那道题(P1265)的弱化改编版。直接跑 spfa 不就可以了吗。

C++
#include<bits/stdc++.h>
using namespace std;
double x[114514], y[114514];
vector<int> G[114514];
int n, m, s, t;
queue<int> q;
double dis[114514];
double jl(double x, double y, double xx, double yy){
	return sqrt((x - xx) * (x - xx) + (y - yy) * (y - yy));
}
int main(){
//	ios::sync_with_stdio(false);
//	cin.tie(0);cout.tie(0);
	cin >> n;
	for (int i = 1; i <= n; i++)
		cin >> x[i] >> y[i];
	cin >> m;
	for (int i = 1; i <= m; i++){
		int u, v;
		cin >> u >> v;
		G[u].push_back(v);
		G[v].push_back(u);
	}
	cin >> s >> t;
	q.push(s);
	for (int i = 1; i <= n; i++)
		dis[i] = 11451419198.00;
	dis[s] = 0.0;
	while(!q.empty()){
		int u = q.front();
		q.pop();
		for (auto v : G[u])
			if (dis[v] > dis[u] + jl(x[u], y[u], x[v], y[v])){
				dis[v] = dis[u] + jl(x[u], y[u], x[v], y[v]);
				q.push(v);
			}
	}
	printf("%.2lf", dis[t]);
}

B

好题。

但是因为二次函数,图像是很明显的(单调),所以直接往优先队列里放,如果太大了直接把这个魔法师踢掉就完事了。

C++
#include<bits/stdc++.h>
using namespace std;
int n, m;
int a[100005], b[100005], c[100005], t[100005];
priority_queue<long long> q;
stack<long long> s;
queue<int> nq;
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	cin >> n >> m;
	for (int i = 1; i <= n; i++)
		cin >> a[i] >> b[i] >> c[i];
	for (int i = 1; i <= n; i++)
		nq.push(i);
	while (!nq.empty()){
		int u = nq.front();
		nq.pop();
		t[u]++;
		long long num = 1ll * a[u] * t[u] * t[u] + 1ll * b[u] * t[u] + c[u];
		if (q.size() < m || q.top() > num){
			q.push(num);
			while (q.size() > m)
				q.pop();
			nq.push(u);
		}
	}
	while (!q.empty()){
		s.push(q.top());
		q.pop();
	}
	while (!s.empty()){
		cout << s.top() << " ";
		s.pop();
	}
}

C

小模拟,但是比较细节。

直接并查集维护,判断是否不属于同一个集合,如果同一个集合就往上跳跳跳。

做完了。注意别路径压缩。

C++
#include<bits/stdc++.h>
#include<bits/extc++.h>
using namespace std;
using namespace __gnu_pbds;
int n;
string a, b;
gp_hash_table<string, string> f;
string csh[1001];
int cnt;
string find(string x){
	if (f[x] == x)
		return x;
	else
		return find(f[x]);
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	cin >> n >> a >> b;
	for (int i = 1; i <= n; i++){
		string u, v;
		cin >> u >> v;
		f[v] = u;
        csh[++cnt] = u;
        csh[++cnt] = v;
	}
    for (int i = 1; i <= cnt; i++){
        if (f[csh[i]] == "")
            f[csh[i]] = csh[i];
    }
    if (f[a] == "" || f[b] ==  ""){
        cout << "NOT RELATED";
        return 0;
    }
	if (f[a] == f[b])
		cout << "SIBLINGS";
	else{
		string la = find(a), lb = find(b);
		if (la != lb)
			cout << "NOT RELATED";
		else {
			string ta = f[a], tb = f[b];
			string ans = "";
			int fda = 1;
			if (f[a] == b)
				cout << b << " is the mother of " << a;
			else if(f[b] == a)
				cout << a << " is the mother of " << b;
            else if (f[f[a]] == f[b])
				cout << b << " is the aunt of " << a;
			else if(f[f[b]] == f[a])
				cout << a << " is the aunt of " << b;
			else
				fda = 0;
			if (fda)
				return 0;
			while (1){
				ta = f[ta];
				tb = f[tb];
				fda = 1;
				if(tb == a)
					cout << a << " is the " << ans << "grand-mother of " << b;
				else if(ta == b)
					cout << b << " is the " << ans << "grand-mother of " << a;
				else if(ta == f[b])
					cout << b << " is the " << ans << "aunt of " << a;
				else if(tb == f[a])
					cout << a << " is the " << ans << "aunt of " << b;
				else
					fda = 0;
				if (fda)
					return 0;
                ans += "great-";
                if (f[ta]==ta && f[tb] == tb)
                    break;
			}
			cout << "COUSINS";
		}
	}
}

D

最小生成树板子。

C++
#include<bits/stdc++.h>
using namespace std;
struct edge{
	public:
		int u, v, value;
};
edge p[1145141];
bool cmp(edge a, edge b){
	return a.value < b.value;
}
int f[114514];
int n, m, ans;
int find(int x){
	if (f[x] == x)
		return x;
	return f[x] = find(f[x]);
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	cin >> n;
	for (int u = 1; u <= n; u++)
		for (int v = 1; v <= n; v++){
			p[++m].u = u;
			p[m].v = v;
			cin >> p[m].value;
		}
	sort(p + 1, p + 1 + m, cmp);
	for (int i = 1; i <= n; i++)
		f[i] = i;
	for (int i = 1; i <= m; i++){
		int fu = find(p[i].u), fv = find(p[i].v);
		if (fu != fv){
			f[fu] = fv;
			ans += p[i].value;
		}
	}
	cout << ans;
}

于是我 1h 1min 37s AK。

U tql%%%

原文