跳到正文

个人记录

7.17比赛题解

xiaojingyvn约 11 分钟
原文
文章信息与版本

文章信息

收录
2026年7月19日
更新
2026年7月19日
篇幅
约 11 分钟

版本

  1. v1当前

7.17比赛题解

t1.Genealogical tree

题目大意

⽕星⼈的⾎缘关系简直乱成⼀锅粥。实际上,⽕星⼈想什么时候发芽就什么时候发芽,想在哪⼉发芽就在哪⼉发芽。他们会聚成各种各样的⼩团体,所以⼀个⽕星⼈可能只有⼀个“爹”,也可能有⼗个。看到⼀百个孩⼦也没⼈会惊讶。⽕星⼈们早已习惯了这种⽣活⽅式,觉得这很⾃然。⽽在星际议会⾥,这套乱七⼋糟的家谱系统就让⼤家头⼤了。这⾥聚集的是最有分量的⽕星⼈,为了不冒犯谁,讨论时总是先让年⻓的⽕星⼈发⾔,再是年轻的,最后才是最年轻、没有孩⼦的评议员。不过,维护这样的发⾔顺序可不是件简单事。毕竟,⽕星⼈也不⼀定知道⾃⼰所有的“爹妈”(更别提爷爷奶奶了!)。但如果不⼩⼼让孙⼦先发⾔,⽽他的曾孙辈却后发⾔,那可真是场⼤丑闻。你的任务就是写个程序,帮⼤家定个规矩,保证每位议员都⽐他的后代先发⾔,永远不会出错。

题目解析

模板题,直接写一个拓扑排序就行

易错点

无

正解代码

C++
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 105;
int n, ind[MAXN];
vector <int> g[MAXN];
void toposort() {
	queue <int> q;
	for (int i = 1; i <= n; i ++) {
		if (ind[i] == 0) q.push(i);
	}
	while (q.size()) {
		int u = q.front();
		q.pop();
		cout << u << ' ';
		for (auto e : g[u]) {
			ind[e] --;
			if (ind[e] == 0) q.push(e);
		}
	}
	return;
}
int main () {
	cin >> n;
	for (int i = 1; i <= n; i ++) {
		int x;
		cin >> x;
		while (x != 0) {
			g[i].push_back(x);
			ind[x] ++;
			cin >> x;
		}
	}
	toposort();
	return 0;
}
C++
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 105;
int n, ind[MAXN];
vector <int> g[MAXN];
void toposort() {
	queue <int> q;
	for (int i = 1; i <= n; i ++) {
		if (ind[i] == 0) q.push(i);
	}
	while (q.size()) {
		int u = q.front();
		q.pop();
		cout << u << ' ';
		for (auto e : g[u]) {
			ind[e] --;
			if (ind[e] == 0) q.push(e);
		}
	}
	return;
}
int main () {
	cin >> n;
	for (int i = 1; i <= n; i ++) {
		int x;
		cin >> x;
		while (x != 0) {
			g[i].push_back(x);
			ind[x] ++;
			cin >> x;
		}
	}
	toposort();
	return 0;
}

t2.Following Orders

题目大意

顺序是数学和计算机科学中的⼀个重要概念。例如,佐恩引理指出:“⼀个每个链都有上界的偏序集包含⼀个极⼤元素。”顺序在推理程序的固定点语义时也很重要。这个问题既不涉及佐恩引理,也不涉及固定点语义,但涉及顺序。给定形式为 x < y 的变量约束列表,您需要编写⼀个程序,打印所有与这些约束⼀致的变量排序。例如,给定约束 x < y 和 x < z,变量 x、y 和 z 有两个与这些约束⼀致的排序:x y z 和 x z y。

题目解析

在拓扑序的基础下进行一次全排列就行

易错点

一定要保持拓扑序

正解代码

C++
#include <bits/stdc++.h>
using namespace std;
int a[30], g[30][30], n, res[30];
bool vis[30];
string s;
bool check(int x, int m) {
    for (int i = 0; i < m; i ++) {
        if (g[x][res[i]]) return false;
    }
    return true;
}
void dfs(int step) {
    if (step == n) {
        for(int i = 0; i < n; i ++) cout << char(res[i] + 'a');
        cout << "\n";
        return;
    }
    for (int i = 0; i < 26; i ++) {
        if (a[i] && !vis[i] && check(i, step)) {
            vis[i] = true;
            res[step] = i;
            dfs(step + 1);
            vis[i] = false;
        }
    }
    return;
}
int main() {
    while (getline(cin, s)) {
        memset(a, 0, sizeof(a));
        memset(g, 0, sizeof(g));
        memset(vis, 0, sizeof(vis));
        n = 0;
        for (int i = 0; i < s.size(); i += 2) {
        	n ++;
			a[s[i] - 'a'] = 1;
        }
        getline(cin, s);
        for (int i = 0; i < s.size(); i += 4) g[s[i] - 'a'][s[i + 2] - 'a'] = 1;
        dfs(0);
        cout << "\n";
    }
    return 0;
}

t3. Substring

题目大意

给定⼀个包含 个节点和 条有向边的图。每个节点被分配⼀个⼩写字⺟。我们定义路径的值为该路径上出现次数最多的字⺟的数量。例如,如果路径上的字⺟是”abaca”,那么该路径的值为 。你的任务是找到值最⼤的路径。

题目解析

用一个数组来记录走过的每一个字母个数,然后打擂台找最大值,注意要用拓扑序来跑一遍,也是非常简单

易错点

无

正解代码

C++
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 3e5 + 5;
int n, m, a[MAXN], ind[MAXN], f[MAXN][26];
vector <int> g[MAXN];
string s;
queue <int> q;
int main() {
    cin >> n >> m >> s;
    for (int i = 0; i < s.size(); i ++) a[i + 1] = s[i] - 'a';
    for (int i = 1; i <= m; i ++) {
    	int u, v;
        cin >> u >> v;
        g[u].push_back(v);
        ind[v] ++;
    }
    for (int i = 1; i <= n; i ++) {
        if (!ind[i]) q.push(i);
    }
    while (q.size()) {
        int u = q.front();
        q.pop();
        f[u][a[u]] ++;
        for (auto e : g[u]) {
            for (int j = 0; j < 26; j ++) f[e][j] = max(f[e][j], f[u][j]);
            ind[e]--;
            if (!ind[e]) q.push(e);
        }
    }
    for (int i = 1; i <= n; i ++) {
        if (ind[i]) {
            cout << -1;
            return 0;
        }
    }
    int ans = 0;
    for (int i = 1; i <= n; i ++) {
        for (int j = 0; j < 26; j ++) ans = max(ans, f[i][j]);
    }
    cout << ans;
    return 0;
}

t4.Coprocessor

题目大意

你有⼀个程序,它由⼀组任务组成,这些任务通过依赖关系图连接起来。这个依赖关系图是⼀个有向⽆环图:每个任务可能依赖⼀个或多个其他任务的结果,且不存在循环依赖。只有当所有依赖的任务都完成后,当前任务才能执⾏。图中的部分任务只能在协处理器上执⾏,剩下的只能在主处理器上执⾏。⼀次协处理器调⽤可以发送⼀组只能在协处理器上执⾏的任务。对于这组任务中的每个任务,它依赖的所有任务必须已经完成,或者也包含在这组任务⾥。主处理器负责启动程序执⾏,并⾃动获取协处理器执⾏任务的结果。请你找出执⾏该程序所需的最少协处理器调⽤次数。

题目解析

一道贪心题,但要按照拓扑序跑,主不需要消耗调用次数,所以先主任务。然后再跑协,用一次协,答案就要+1

易错点

无

正解代码

C++
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 7;
vector <int> v[N], ans;
queue <int> q1, q2;
int in[N];
bool e[N];
int n, m;

void topo() {
	for (int i = 1; i <= n; ++i) {
		if (!in[i]) {
			if (e[i] == 0) q1.push(i);
			else q2.push(i);
		}
	}
	int ans = 0;
	while (!q1.empty() || !q2.empty()) { 
		while (!q1.empty()) { 
			int H = q1.front();
			q1.pop();
			int len = v[H].size();
			for (int i = 0; i < len; ++i) {
				int u = v[H][i];
				in[u]--;
				if (!in[u]) {
					if (e[u] == 0) q1.push(u);
					else q2.push(u);
				}
			}
		}
		if (!q2.empty()) ans++;
		while (!q2.empty()) {

			int H = q2.front();
			q2.pop();
			int len = v[H].size();
			for (int i = 0; i < len; ++i) {
				int u = v[H][i];
				in[u]--;
				if (!in[u]) {
					if (e[u] == 0) q1.push(u);
					else q2.push(u);
				}
			}
		}
	}
	cout << ans;
	return ;
}

int main() {
	cin >> n >> m;
	for (int i = 1; i <= n; ++i) {
		cin >> e[i];
	}
	for (int i = 1, x, y; i <= m; ++i) {
		cin >> x >> y;
		x++, y++;
		v[x].push_back(y);
		in[y]++;
	}
	topo();
	return 0;
}
原文