CSP模拟XX/2022赛前模测 提高组验题-14
A 数组复原 / 快速De变换
发现一组 \(s1_i\) 和 \(s2_i\) 可以看作 \(s1_i\) 与 \(s2_i\) 之间连有一条边
那么我们就是要找一条欧拉路,然后我不会。。。
做法其实就是找到度为奇数的点开始 \(DFS\) ,然后扫完了再记录
比较坑的就是题目没有保证数据合法,也就是说存在 \(s1 \geq s2\) 那么应该判为 \(-1\)
新数据 \(By\) \(Delov\) 大佬,卡掉了每次爆扫的做法,需要加入类似网络流当前弧优化的东西但是直接加也是不对的
code
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 200005;
inline int read(){
int x = 0; char c = getchar();
while(c < '0' || c > '9')c = getchar();
do {x = (x << 3) + (x << 1) + (c ^ 48); c = getchar();}while(c <= '9' && c >='0');
return x;
}
int n, ans[maxn], p;
struct node{int s1, s2;}d[maxn];
int head[maxn << 1 | 1], tot = 1;
struct edge{
int to, net;
}e[maxn << 1 | 1];
int rd[maxn];
void add(int u, int v){
e[++tot].net = head[u];
head[u] = tot;
e[tot].to = v;
}
int val[maxn];
unordered_map<int, int>id;
int cnt;
bool vis[maxn << 1 | 1];
void dfs(int x){
while(head[x]){
int i = head[x];
head[x] = e[i].net;
if(!vis[i]){
vis[i] = vis[i ^ 1] = 1;
dfs(e[i].to);
}
}
ans[++p] = val[x];
}
int main(){
n = read(); --n;
for(int i = 1; i <= n; ++i)d[i].s1 = read();
for(int i = 1; i <= n; ++i)d[i].s2 = read();
bool f = 0;
for(int i = 1; i <= n; ++i){
if(id[d[i].s1] == 0)id[d[i].s1] = ++cnt, val[cnt] = d[i].s1;
if(id[d[i].s2] == 0)id[d[i].s2] = ++cnt, val[cnt] = d[i].s2;
if(d[i].s1 > d[i].s2){
f = 1;
break;
}
add(id[d[i].s1], id[d[i].s2]);
add(id[d[i].s2], id[d[i].s1]);
++rd[id[d[i].s1]]; ++rd[id[d[i].s2]];
}
int s = 1, c = 0;
for(int i = 1; i <= cnt; ++i)if(rd[i] & 1)s = i, ++c;
dfs(s);
if(p < n + 1 || c == 1 || c > 2 || f)printf("-1\n");
else for(int i = 1; i <= n + 1; ++i)printf("%d ",ans[i]);
return 0;
}
B 最小正环 / 散步
这没啥说的,倍增即可
坑点在于环长没有单调性,也就是说你走 \(2k\) 步不一定比走 \(k\) 步优,所以倍增过程需要取本次和上次的 \(max\)
新数据 \(By\) \(joke3579\) 好像卡掉了不少复杂度不对的解法
code
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 505;
const int inf = 0x3f3f3f3f;
inline int read(){
int x = 0; bool f = 0; char c = getchar();
while(c < '0' || c > '9'){if(c == '-')f = 1;c = getchar();}
do {x = (x << 3) + (x << 1) + (c ^ 48); c = getchar();}while(c <= '9' && c >='0');
return f ? -x : x;
}
int n, m;
int mp[maxn][maxn], tmp[maxn][maxn];
int bz[45][maxn][maxn];
int main(){
n = read(); m = read();
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= n; ++j)
mp[i][j] = -inf;
for(int i = 1; i <= m; ++i){
int u = read(), v = read(), w1 = read(), w2 = read();
mp[u][v] = max(mp[u][v], w1);
mp[v][u] = max(mp[v][u], w2);
}
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= n; ++j)
bz[0][i][j] = mp[i][j];
bool fl = 1;
for(int i = 1; i <= n; ++i)if(bz[0][i][i] > 0)fl = 0;
int ans = 1 + fl;
int k = 0;
for(; fl && k <= 30;){
++k;
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= n; ++j)
bz[k][i][j] = bz[k - 1][i][j];
for(int i = 1; i <= n; ++i)
for(int p = 1; p <= n; ++p)
for(int j = 1; j <= n; ++j)
bz[k][i][j] = max(bz[k][i][j], bz[k - 1][i][p] + bz[k - 1][p][j]);
for(int i = 1; i <= n; ++i)if(bz[k][i][i] > 0)fl = 0;
}
if(fl){
printf("0\n");
return 0;
}
for(int now = k - 1; now >= 0; --now){
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= n; ++j)
tmp[i][j] = -inf;
for(int i = 1; i <= n; ++i)
for(int p = 1; p <= n; ++p)
for(int j = 1; j <= n; ++j)
tmp[i][j] = max(tmp[i][j], bz[now][i][p] + mp[p][j]);
bool fl = 1;
for(int i = 1; i <= n; ++i)if(tmp[i][i] > 0)fl = 0;
if(fl){
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= n; ++j)
mp[i][j] = tmp[i][j];
ans += (1 << now);
}
}
printf("%d\n", ans);
return 0;
}
C 修水管
由于是我造的新数据,所以讲的详细些
D 小A的游戏 (原 T4)
考试原题弱化
code
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn = 100005;
inline int read(){
int x = 0; char c = getchar();
while(c < '0' || c > '9')c = getchar();
do{x = (x << 3) + (x << 1) + (c ^ 48); c = getchar();}while(c <= '9' && c >= '0');
return x;
}
struct edge{
int to,val,net;
}e[maxn << 1 | 1];
int head[maxn], tot, n, rd[maxn], m;
void add(int u, int v, int w){
e[++tot].net = head[u];
head[u] = tot;
e[tot].to = v;
e[tot].val = w;
++rd[v];
}
int sum[maxn], fa[maxn], son[maxn], size[maxn], top[maxn], dep[maxn], id[maxn];
void dfs1(int x){
size[x] = 1;
for(int i = head[x]; i; i = e[i].net){
int v = e[i].to;
if(v == fa[x])continue;
fa[v] = x; sum[v] = sum[x] + e[i].val;
dep[v] = dep[x] + 1;
dfs1(v);
size[x] += size[v];
son[x] = size[son[x]] >= size[v] ? son[x] : v;
}
}
void dfs2(int x, int tp){
top[x] = tp;
if(son[x])dfs2(son[x], tp);
for(int i = head[x]; i; i = e[i].net){
int v = e[i].to;
if(v == fa[x] || v == son[x])continue;
dfs2(v, v);
}
}
int dis(int u, int v){
int ans = 0;
while(top[u] != top[v]){
if(dep[top[u]] < dep[top[v]])swap(u, v);
ans += sum[u] - sum[fa[top[u]]];
u = fa[top[u]];
}
if(dep[u] < dep[v])swap(u, v);
ans += sum[u] - sum[v];
return ans;
}
struct tree{
struct node{int l, r, dis;}t[maxn << 2 | 1];
node push_up(node ls, node rs){
node ans = ls.dis > rs.dis ? ls : rs;
int dll = dis(ls.l, rs.l), dlr = dis(ls.l, rs.r), drl = dis(ls.r, rs.l), drr = dis(ls.r, rs.r);
if(dll > ans.dis)ans = {ls.l, rs.l, dll};
if(dlr > ans.dis)ans = {ls.l, rs.r, dlr};
if(drl > ans.dis)ans = {ls.r, rs.l, drl};
if(drr > ans.dis)ans = {ls.r, rs.r, drr};
return ans;
}
void built(int x, int l, int r){
if(l == r){
t[x] = {l, l, 0};
return;
}
int mid = (l + r) >> 1;
built(x << 1, l, mid);
built(x << 1 | 1, mid + 1, r);
t[x] = push_up(t[x << 1], t[x << 1 | 1]);
}
node query(int x, int l, int r, int L, int R){
if(L <= l && r <= R)return t[x];
int mid = (l + r) >> 1;
if(L <= mid && R > mid)return push_up(query(x << 1, l, mid, L, R), query(x << 1 | 1, mid + 1, r, l, R));
if(L <= mid)return query(x << 1, l, mid, L, R);
return query(x << 1 | 1, mid + 1, r, L, R);
}
int ask(int l, int r){
node ans = query(1, 1, n, l, r);
return ans.dis;
}
}t;
int main(){
n = read(); m = read();
for(int i = 1; i < n; ++i){
int u, v, w;
u = read(), v = read(), w = read();
add(u, v, w);
add(v, u, w);
}
dfs1(1); dfs2(1, 1);
t.built(1, 1, n);
for(int i = 1; i <= m; ++i){
int l = read(), r = read();
printf("%d\n", t.ask(l, r));
}
return 0;
}
不知道起什么名字 (新T4?)
\(gtm1514\) 搬了道 \(CF\) 紫题并且加强了数据范围
具体没看