2025/10/4 模拟赛赛后总结
图论专场
T1
给出n个点m条边的双向图,每次操作你可以重建一条边:重新选择一条边的起点终点。 请问最少需要多少次操作才能将图变为一个连通图,并请你输出字典序最小的方案。
赛时
半小时解决,结果被memset坑了挂分了。
解决
先是去想操作次数,很显然就是连通块个数减一,连通块个数可以用并查集求。然后再去想如何让修改字典序最小,让修改前的边最小,只需要在并查集连接时优先用序号大的边,在连图时用最小的边就可以了,只需要两个sort。让修改后的边最小,我的解决办法是从1到n遍历点,如果有一个点不在1号点所在的树上就把这两个点连起来,当然也可以在并查集的时候就把尽可能小的点作为父亲然后维护堆顶。
代码
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
const int M = 2e5 + 10;
struct node{
int l, r;
}bian[M], input[M];
int fa[N];
int vis[N];
int find(int x){
return fa[x] == x ? x : fa[x] = find(fa[x]);
}
int read(){
int s = 0;
char hc = getchar();
while(hc > '9' || hc < '0') hc = getchar();
while(hc <= '9' && hc >= '0'){
s = (s << 3) + (s << 1) + (hc ^ 48);
hc = getchar();
}
return s;
}
bool cmp1(node a, node b){
return a.l == b.l ? a.r > b.r : a.l > b.l;
}
bool cmp2(node a, node b){
return a.l == b.l ? a.r < b.r : a.l < b.l;
}
int main()
{
//freopen("rebuild.in", "r", stdin);
//freopen("rebuild.out", "w", stdout);
int T, n, m, s, cnt;
cin >> T;
while(T--){
s = 0;
cnt = 0;
n = read();
m = read();
for(int i = 1; i <= n; i++){
fa[i] = i;
}
for(int i = 1; i <= m; i++){
input[i].l = read();
input[i].r = read();
if(input[i].l > input[i].r) swap(input[i].l, input[i].r);
}
sort(input + 1, input + m + 1, cmp1);
for(int i = 1, fj, fk; i <= m; i++){
fj = find(input[i].l);
fk = find(input[i].r);
if(fj != fk){
fa[fj] = fk;
s++;
}
else{
cnt++;
bian[cnt].l = input[i].l;
bian[cnt].r = input[i].r;
}
}
if(m < n - 1){
cout << -1 << "\n";
}
else{
cout << n - s - 1 << "\n";
sort(bian + 1, bian + cnt + 1, cmp2);
for(int i = 1; i <= n; i++){
fa[i] = find(i);
}
cnt = 0;
for(int i = 1; i <= n; i++){
if(!vis[fa[i]] && fa[i] != fa[1]){
vis[fa[i]] = 1;
cnt++;
cout << bian[cnt].l << " " << bian[cnt].r << " " << 1 << " " << i << "\n";
}
}
memset(vis, 0, sizeof(vis));
}
}
return 0;
}
T2
给定n个点的有根树,1为根。每个点有权值w(一定为正)和 a。从根节点出发,以一种随机的方
式走到某一个叶节点。随机方式如下。
当前点为u,不是叶节点。记的所有儿子的w的和为sum,则u走到某一个儿子v的概率为
赛时
发现n方有整整60分,果断选择暴力更新
解决
其实只要在修改的时候维护同父亲的节点的w然后去向上遍历更新就可以了,这里给出两种代码
60分
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
const int mod = 998244353;
struct edge{
int to, next;
}bian[N << 1];
int head[N], cnt = 1;
long long w[N], a[N];
long long sum[N], qz[N];
long long fan(long long a){
int b = mod - 2;
long long ans = 1;
while(b){
if(b & 1){
ans = ans * a % mod;
}
a = a * a % mod;
b >>= 1;
}
return ans;
}
void add(int u, int v){
bian[cnt].to = v;
bian[cnt].next = head[u];
head[u] = cnt;
cnt++;
return;
}
int read(){
int s = 0;
char hc = getchar();
while(hc > '9' || hc < '0') hc = getchar();
while(hc <= '9' && hc >= '0'){
s = (s << 3) + (s << 1) + (hc ^ 48);
hc = getchar();
}
return s;
}
void dfs_1(int u, int fa){
sum[u] = 0;
for(int i = head[u]; i; i = bian[i].next){
int v = bian[i].to;
if(v == fa) continue;
dfs_1(v, u);
sum[u] += w[v];
}
qz[u] = a[u];
for(int i = head[u]; i; i = bian[i].next){
int v = bian[i].to;
if(v == fa) continue;
qz[u] = (qz[u] + qz[v] * w[v] % mod * fan(sum[u] % mod)) % mod;
}
return;
}
int main()
{
freopen("climb.in", "r", stdin);
freopen("climb.out", "w", stdout);
int n, q;
cin >> n;
for(int i = 2, v; i <= n; i++){
v = read();
add(i, v);
add(v, i);
}
for(int i = 1; i <= n; i++){
w[i] = read();
}
for(int i = 1; i <= n; i++){
a[i] = read();
}
dfs_1(1, 0);
cout << qz[1] << "\n";
cin >> q;
for(int i = 1, u1, w1, a1; i <= q; i++){
u1 = read();
w1 = read();
a1 = read();
w[u1] = w1;
a[u1] = a1;
dfs_1(1, 0);
cout << qz[1] << "\n";
}
return 0;
}
100分
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define gc getchar()
#define pb push_back
#define pll pair<ll,ll>
#define val first
#define id second
const ll maxn=1e6+1010,mod=998244353,INF=4e18+1010;
inline void read(ll &x)
{
x=0;char ch=gc;
while(ch<'0'||ch>'9')ch=gc;
while(ch>='0'&&ch<='9')x=(x<<1)+(x<<3)+(ch^48),ch=gc;
}
ll n;
ll fa[maxn];
vector<ll> v[maxn];
ll w[maxn],a[maxn];
ll sumw[maxn],swa[maxn],poi[maxn];
ll q,u,nw,na;
ll qpow(ll num,ll base)
{
ll res=1;
while(base)
{
if(base&1)res=res*num%mod;
base>>=1;
num=num*num%mod;
}
return res;
}
void dfs(ll x,ll pre)
{
for(int i=0;i<v[x].size();i++)
{
if(v[x][i]==pre)continue;
dfs(v[x][i],x);
sumw[x]=(sumw[x]+w[v[x][i]])%mod;
swa[x]=(swa[x]+poi[v[x][i]]*w[v[x][i]]%mod)%mod;
}
poi[x]=(swa[x]*qpow(sumw[x],mod-2)%mod+a[x])%mod;
}
void modify(ll x,ll pre,ll lastpoi,ll lastw)
{
if(x==0)return;
swa[x]=(swa[x]-lastpoi*lastw%mod+poi[pre]*w[pre]%mod+mod)%mod;
ll tmp=poi[x];
poi[x]=(swa[x]*qpow(sumw[x],mod-2)%mod+a[x])%mod;
modify(fa[x],x,tmp,w[x]);
}
int main()
{
//freopen("climb.in","r",stdin);
//freopen("climb.out","w",stdout);
read(n);
for(int i=2;i<=n;i++)
{
read(fa[i]);
v[i].pb(fa[i]);
v[fa[i]].pb(i);
}
for(int i=1;i<=n;i++)read(w[i]);
for(int i=1;i<=n;i++)read(a[i]);
dfs(1,0);
printf("%lld\n",poi[1]);
read(q);
while(q--)
{
read(u);read(nw);read(na);
ll tmp=poi[u];
poi[u]=(poi[u]-a[u]+na+mod)%mod;
sumw[fa[u]]=sumw[fa[u]]-w[u]+nw;
ll tmp2=w[u];
a[u]=na;w[u]=nw;
modify(fa[u],u,tmp,tmp2);
printf("%lld\n",poi[1]);
}
return 0;
}
T3
给一个n个点的无向图,现在有m0条公路和m1条铁路。 Sophie和May从同一个起点u出发去旅行,Sophie只走公路,May只走铁路,请问有多少点Sophie和 May都可以访问到? 请你计算u = 1到n的所有答案
赛时
并查集+dfs暴力拿了60分走人(所以留那么多时间打T4一定是对T4信心满满了对吧哈哈哈哈哈)
解决
这个题面非常容易让人想到染色,因为其要求两人都能走到,所以对于每次查询都是将两个大块取交集的过程,考虑初始化大块间交集中数的个数,即用索引为块的编号的map存储数量
代码
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 10;
int fa1[N], fa2[N];
unordered_map <int, int> s[N];
int find1(int x){
return fa1[x] == x ? x : fa1[x] = find1(fa1[x]);
}
int find2(int x){
return fa2[x] == x ? x : fa2[x] = find2(fa2[x]);
}
int read(){
int s = 0;
char hc = getchar();
while(hc > '9' || hc < '0') hc = getchar();
while(hc <= '9' && hc >= '0'){
s = (s << 3) + (s << 1) + (hc ^ 48);
hc = getchar();
}
return s;
}
int main()
{
//freopen("intersection3.in", "r", stdin);
//freopen("intersection3.out", "w", stdout);
int n, m0, m1;
cin >> n >> m0 >> m1;
for(int i = 1; i <= n; i++){
fa1[i] = i;
fa2[i] = i;
}
for(int i = 1, u, v, fu, fv; i <= m0; i++){
u = read();
v = read();
fu = find1(u);
fv = find1(v);
if(fu != fv){
fa1[fv] = fu;
}
}
for(int i = 1, u, v, fu, fv; i <= m1; i++){
u = read();
v = read();
fu = find2(u);
fv = find2(v);
if(fu != fv){
fa2[fv] = fu;
}
}
for(int i = 1; i <= n; i++){
fa1[i] = find1(i);
fa2[i] = find2(i);
}
for(int i = 1; i <= n; i++){
if(s[fa1[i]].count(fa2[i])) s[fa1[i]][fa2[i]]++;
else s[fa1[i]][fa2[i]] = 1;
}
for(int i = 1; i <= n; i++){
cout << s[fa1[i]][fa2[i]] << " ";
}
return 0;
}
T4
A市的地铁系统由c家公司运营,每家公司都开发了一些地铁线路,这些地铁线路连接了n个地铁站。 线路共有m条,每条双向边有三个属性,代表这条线路连接u,v两站,由c公司运营。 地铁系统的收费规则为:每家公司都有单独的地铁票,售价为p[i],一张票允许你 "不间断" 地任意乘坐 这家公司的地铁线路,直到你到达终点或者换乘其他公司的线路。 也就是说:对于路径上相邻的边(u,v,c1)和(v,z,c2):若c1=c2则不花钱,否则需要花费p[c2]重新买票,当然从起点上车时也要买票。 现在给定n和m条边,以及p数组,请你计算从起点1到所有点所需的最小花费。
赛时
奇妙思路直接拿下最优解(但是因为跳出dfs的条件写错了挂了…………)
解决
直接跑dijkstra,对于每一次遍历到的边进行dfs去更新能通过同一家公司连到的点的权值,并将跟这些点相连的边加入到优先队列中,将这些点视为已遍历过。
代码
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
const int M = 2e5 + 10;
const int P = 1e6 + 10;
int p[P], qz[N], head[N], cnt = 1;
struct edge{
int pre, to, next, c;
friend bool operator < (edge a, edge b){
return qz[a.pre] + p[a.c] > qz[b.pre] + p[b.c];
}
}bian[M << 1];
bool vis[N];
priority_queue <edge> q;
void add(int u, int v, int c){
bian[cnt].pre = u;
bian[cnt].to = v;
bian[cnt].next = head[u];
bian[cnt].c = c;
head[u] = cnt;
cnt++;
return;
}
int read(){
int s = 0;
char hc = getchar();
while(hc > '9' || hc < '0') hc = getchar();
while(hc <= '9' && hc >= '0'){
s = (s << 3) + (s << 1) + (hc ^ 48);
hc = getchar();
}
return s;
}
void dfs(int z, int u, int c, int fa){
for(int i = head[u]; i; i = bian[i].next){
int v = bian[i].to;
if(v == fa) continue;
if(bian[i].c == c){
qz[v] = min(z, qz[v]);
vis[v] = 1;
dfs(z, v, c, u);
}
else{
if(vis[v]) continue;
q.push(bian[i]);
}
}
return;
}
int main()
{
//freopen("subway.in", "r", stdin);
//freopen("subway.out", "w", stdout);
int n, m, C;
cin >> n >> m >> C;
for(int i = 1; i <= C; i++) p[i] = read();
for(int i = 1, u, v, c; i <= m; i++){
u = read();
v = read();
c = read();
add(u, v, c);
add(v, u, c);
}
memset(qz, 0x3f, sizeof(qz));
qz[1] = 0;
vis[1] = 1;
for(int i = head[1]; i; i = bian[i].next){
q.push(bian[i]);
}
while(!q.empty()){
edge now = q.top();
q.pop();
if(qz[now.to] > qz[now.pre] + p[now.c]){
qz[now.to] = qz[now.pre] + p[now.c];
}
vis[now.to] = 1;
dfs(qz[now.pre] + p[now.c], now.to, now.c, now.pre);
}
for(int i = 1; i <= n; i++){
if(qz[i] == 0x3f3f3f3f) cout << -1 << " ";
else cout << qz[i] << " ";
}
return 0;
}