“华为杯“华南理工大学程序设计竞赛(同步赛)

目录

最长回文子串

又一道好题

信号塔

网格树

异色边2


|---|--------------------------------------------------------------------|
| M | ### 最长回文子串 |

构造+马拉车

cpp 复制代码
#define int long long//__int128 2^127-1(GCC)
#define PII pair<int,int>
//#define f first
//#define s second
const int inf = 0x3f3f3f3f3f3f3f3f, N = 1e5 + 5, mod = 1e9 + 7;
signed main()
{
	ios_base::sync_with_stdio(0); cin.tie(0), cout.tie(0);
	int T;
	cin >> T;
	while (T--)
	{
		int n;
		cin >> n;
		vector<int>a(n + 1);
		for (int i = 1; i <= n; i++) cin >> a[i];
		vector<int>f(n + 1);
		f[1] = f[2] = 1;
		for (int i = 2; i < n; i++) {
			if (a[i] == 1) {
				f[i + 1] = 1 - f[i - 1];
			}
			else {
				f[i + 1] = f[i - 1];
			}
		}
		vector<int>tmp(n * 2 + 5);
		int cnt = 0;
		tmp[++cnt]=-2, tmp[++cnt] = -1;
		for (int i = 1; i <= n; i++) {
			tmp[++cnt] = f[i];
			tmp[++cnt] = -1;
		}
		tmp[++cnt] = -3;
		int mr = 0, mid = 0, ans = 0;
		vector<int>p(n * 2 + 5);
		for (int i = 2; i <= cnt - 1; i++) {
			//mid-(i-mid)
			if (i < mr)p[i] = min(p[mid - (i - mid)], mr - i + 1);
			else p[i] = 1;
			while (tmp[i - p[i]] == tmp[i + p[i]]) ++p[i];
			if (i + p[i] > mr) mr = i + p[i] - 1, mid = i;
		}
		bool ok = true;
		for (int i = 3; i <= cnt - 2; i += 2) {
			if ((p[i] / 2-1)*2+1 != a[i/2])ok = false;
		}
		if (ok) {
			for (int i = 1; i <= n; i++) {
				cout << f[i];
			}
			cout << "\n";
		}
		else {
			cout << -1 << '\n';
		}
	}
}

|---|------------------------------------------------------------------|
| K | ### 又一道好题 |

启发式合并

cpp 复制代码
int g[N], idx[N], val[N], m;
unordered_set<int>st[N];
int getid(int v) {
	if (g[v] == -1) {
		g[v] = ++m;
		val[g[v]] = v;
	}
	return g[v];
}
void join(int x, int y, int v) {
	val[x] = v;
	g[v] = x;
	for (int w : st[y]) {
		st[x].insert(w);
		idx[w] = x;
	}
	st[y].clear();
	g[v - 1] = -1;
}
signed main()
{
	ios_base::sync_with_stdio(0); cin.tie(0), cout.tie(0);
	int T;
	cin >> T;
	while (T--)
	{
		int n, Q;
		cin >> n >> Q;
		for (int i = 0; i <= Q; ++i) g[i] = -1;
		for (int i = 1; i <= n; ++i) {
			st[0].insert(i);
			idx[i] = 0;
		}
		g[0] = 0;
		val[0] = 0;
		m = 0;
        int QQ=Q;
		while (Q--)
		{
			int op, x;
			cin >> op >> x;
			if (op == 1) {
				int p = idx[x];
				int v = val[p] + 1;
				st[p].erase(x);
				int w = getid(v);
				st[w].insert(x);
				idx[x] = w;
			}
			else {
				if (g[x] == -1)  continue;
				int x1 = g[x];
				int x2 = getid(x + 1);
				if (st[x1].size() < st[x2].size()) {
					join(x2, x1, x + 1);
				}
				else {
					join(x1, x2, x + 1);
				}
			}
		}
		for (int i = 1; i <= n; i++) {
			cout << val[idx[i]] << ' ';
		}
		cout << "\n";
		for (int i = 0; i <= QQ; i++) {
			st[i].clear();
		}
	}
}

|---|--------------------------------------------------------------|
| H | ### 信号塔 |

换根dp

dp 0表示当前没有信号塔子树最少需要信号塔数,1 当前有信号塔子树需要的信号塔数

cpp 复制代码
#define int long long//__int128 2^127-1(GCC)
#define PII pair<int,int>
//#define f first
//#define s second
const int inf = 0x3f3f3f3f3f3f3f3f, N = 2e5 + 5, mod = 1e9 + 7;
vector<int>q[N];
PII dp[N];//0 no 1 yes
int sz[N];
int f[N];
void dfs(int x, int fa)
{
	f[x] = fa;
	sz[x] = 1;
	dp[x] = { 0,1 };
	for (auto w : q[x]) {
		if (w == fa) continue;
		dfs(w, x);
		sz[x] += sz[w];
		dp[x].first += dp[w].second;
		dp[x].second += min(dp[w].first, dp[w].second);
	}
}
map<PII, int>mp;
int res[N];
void dfs2(int x, int fa) {
	for (auto w : q[x]) {
		if (w == fa) continue;
		dp[x].first -= dp[w].second;
		dp[x].second -= min(dp[w].first, dp[w].second);
		dp[x].first += dp[fa].second;
		dp[x].second += min(dp[fa].first, dp[fa].second);
		res[mp[{min(x, w), max(x, w)}]] = min(dp[x].first, dp[x].second) + min(dp[w].first, dp[w].second);
		dfs2(w, x);
		dp[x].second-= min(dp[fa].first, dp[fa].second);
		dp[x].first-= dp[fa].second;
		dp[x].first += dp[w].second;
		dp[x].second += min(dp[w].first, dp[w].second);
	}
}
signed main()
{
	ios_base::sync_with_stdio(0); cin.tie(0), cout.tie(0);
	int T;
	cin >> T;
	while (T--)
	{
		int n;
		cin >> n;
		for (int i = 1; i <= n; i++) q[i].clear();
		for (int i = 1; i < n; i++)
		{
			int u, v;
			cin >> u >> v;
			mp[{min(u, v), max(u, v)}] = i;
			q[u].push_back(v);
			q[v].push_back(u);
		}
		dfs(1, 0);
		dfs2(1, 0);
		for (int i = 1; i < n; i++) {
			cout << n - res[i] << ' ';
		}
		cout << "\n";
		mp.clear();
	}
}

|---|--------------------------------------------------------------|
| G | ### 网格树 |

暴力dfs+剪枝

cpp 复制代码
#define int long long//__int128 2^127-1(GCC)
#define PII pair<int,int>
const int inf = 0x3f3f3f3f3f3f3f3f, N = 1e5 + 5, mod = 1e9 + 7;
int i, j, k, n, m, t, tot, cur, li, xx, yy;
vector<pair<int, int> > d = { {1,0},{-1,0},{0,1},{0,-1} };
string s[66];
int res, f[33][33], op[33][33], vis[33][33];
bool dfs2(int x, int y, int fx, int fy) {//用来检查当前放置方法是否符合
	if (vis[x][y]) return 0;
	vis[x][y] = 1; tot++;
	for (auto w : d) {
		int i = w.first, j = w.second;
		i += x, j += y;
		if (i<1 || i>n || j<1 || j>m || f[i][j]==0) continue;
		if (i == fx && j == fy) continue;
		if (dfs2(i, j, x, y)==0) return 0;
	}
	return 1;
}
void dfs(int x, int y)
{
	memset(vis, 0, sizeof vis); tot = 0;
	if (y > m) { x++; y = 1; }
	int sb = n * m - (x - 1) * m - (y - 1);
	if (sb + cur <= res)return;
	if (x > n) {//终止条件
		if (cur <= res || dfs2(xx, yy, -1, -1)==0) return;
		if (tot != cur) return;
		memcpy(op, f, sizeof f); res = cur;//op当前最优的放置方法
		return;
	}

	f[x][y] = 1; cur++;//f=1表示当前点位'.'  0表示'#'
	if (dfs2(x, y, -1, -1)==1&&tot==cur)dfs(x, y + 1);
	f[x][y] = 0; cur--;

	if (s[x][y] != '+')dfs(x, y + 1);
}
signed main()
{
	ios_base::sync_with_stdio(0); cin.tie(0), cout.tie(0);
	cin >> n >> m;
	for (int i = 1; i <= n; i++) {
		s[i] = string(m, '.');
		s[i] = "$" + s[i];
	}
	xx = yy = 1; s[1][1] = '+';//表示以1 1为跟的树
	dfs(1, 1);
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			if (op[i][j]) cout << ".";
			else cout << "W";
		}
		cout << "\n";
	}
}

|---|----------------------------------------------------------------|
| J | ### 异色边2 |

就是特殊处理一下环的元素,找到环后其他就都是树,贪心一下就行了,有点像基环树,找环的时候简单用一下tarjan

cpp 复制代码
#define int long long//__int128 2^127-1(GCC)
#define PII pair<int,int>
const int inf = 0x3f3f3f3f3f3f3f3f, N = 3e5 + 5, mod = 1e9 + 7;
vector<int>v[N];
int f[N];
int cnt, top;
int k;
int sk[N];
int h[N];
int col[N];
void dfs(int x,int fa)
{
    sk[++top]=x;
    f[x]=1;
    for(int y:v[x]){
        if(y==fa) continue;
        if(f[y]==1){
            if(cnt!=0){
                continue;
            }
            for(int i=top;sk[i]!=y&&i>=1;i--)//找到唯一的环 
                h[++cnt]=sk[i];
            h[++cnt]=y;
        }else dfs(y,x);
    }
    top--;
}
void dfs1(int x,int fa)
{
    for(int y:v[x]){
        if(y==fa||!f[y]) continue;
        if(k>0){
            k--;
            col[y]=col[x]^1;
        }else col[y]=col[x];
        dfs1(y,x);
    }
}
signed main()
{
    ios_base::sync_with_stdio(0); cin.tie(0), cout.tie(0);
    int T;
    cin >> T;
    while (T--)
    {
        int n;
        cin >> n >> k;
        for (int i = 1; i <= n; i++) v[i].clear(), f[i] = 0;
        for (int i = 1; i <= n; i++) {
            int a, b;
            cin >> a >> b;
            v[a].push_back(b);
            v[b].push_back(a);
        }
        cnt = top = 0;
        dfs(1,0);
        int t=cnt/2*2;//如果时是奇数的话,有一个是没用的 
        if(t<=k){
            for(int i=1;i<=cnt;i++){
                col[h[i]]=i&1;
            }
            k-=t;
        }else{
            t=k/2*2;
            for(int i=1;i<=t;i++){
                col[h[i]]=i&1;
            }
            for(int i=t+1;i<=cnt;i++){
                col[h[i]]=1;
            }
            k%=2;
        }
        for(int i=1;i<=cnt;i++){
            f[h[i]]=0;
        }
        for(int i=1;i<=cnt;i++){
            dfs1(h[i],0);
        }
        if(k>0){
            cout<<"-1\n";
        }else{
            for(int i=1;i<=n;i++){
                cout<<col[i];
            }
            cout<<"\n";
        }
    }
}
相关推荐
智慧化智能化数字化方案3 小时前
华为IPD流程管理体系L1至L5最佳实践-解读
大数据·华为
ZZZCY20034 小时前
华为VER系统及CLI命令熟悉
华为
SuperHeroWu718 小时前
【HarmonyOS】鸿蒙应用接入微博分享
华为·harmonyos·鸿蒙·微博·微博分享·微博sdk集成·sdk集成
期待未来的男孩18 小时前
华为FusionCube 500-8.2.0SPC100 实施部署文档
华为
岳不谢20 小时前
VPN技术-VPN简介学习笔记
网络·笔记·学习·华为
郝晨妤2 天前
鸿蒙原生应用开发元服务 元服务是什么?和App的关系?(保姆级步骤)
android·ios·华为od·华为·华为云·harmonyos·鸿蒙
柳如烟@2 天前
华为Ensp模拟器配置RIP路由协议
华为
jikuaidi6yuan3 天前
鸿蒙系统简介
华为·harmonyos
何遇mirror3 天前
【话题】抓住鸿蒙生态崛起的机遇:挑战与对策
华为·harmonyos
LKAI.3 天前
华为VPN技术
运维·网络·安全·华为·智能路由器