题目

  • 1002 Independent Feedback Vertex Set
    • AC代码
  • 1003 Counting Stickmen
    • AC代码

1002 Independent Feedback Vertex Set

**Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 346 Accepted Submission(s): 210
**

Problem Description

Yukikaze loves graph theory, especially forests and independent sets.

  • Forest: an undirected graph without cycles.
  • Independent set: a set of vertices in a graph such that for every two vertices, there is no edge connecting the two.

Yukikaze has an undirected graph G=(V,E) where V is the set of vertices and E is the set of edges. Each vertex in V has a vertex weight. Now she wants to divide V into two complementary subsets VI and VF such that VI is an independent set, and the induced subgraph G[VF] is a forest. The induced subgraph G[VF] is the graph whose vertex set is VF and whose edge set consists of all of the edges in E that have both endpoints in VF. In addition, she wants to maximize the sum of weights of vertices in VI.

Since this problem is NP-hard for general graphs, she decides to solve a special case of the problem. We can build a special graph by the following steps. Initially, the graph consists of three vertices 1,2,3 and three edges (1,2),(2,3),(3,1). When we add a vertex x into the graph, we select an edge (y,z) that already exists in the graph and connect (x,y) and (x,z). Keep doing this until there are n vertices in the graph.

Input

The first line of the input contains a single integer T (1≤T≤103), indicating the number of test cases.

The first line of each test case contains a single integer n (4≤n≤105), indicating the number of vertices in the graph. It is guaranteed that the sum of n over all test cases won’t exceed 106.

The second line of each test case contains n positive integers a1,a2,…,an (1≤ai≤109), indicating the weights of the vertices.

Initially, the graph consists of three vertices 1,2,3 and three edges (1,2),(2,3),(3,1). The i-th line of the next n−3 lines contains two integers u,v (1≤u,v<i+3), indicating the addition of a vertex i+3 and two edges (i+3,u),(i+3,v) to the graph. It is guaranteed that (u,v) already exists in the graph.

Output

For each test case, print an integer in a single line indicating the maximum sum of weights of vertices in VI.

Sample Input

3
4
3 3 2 2
1 2
4
2 5 5 2
2 3
5
3 1 1 1 1
1 2
1 3

Sample Output

4
5
3

Source

2022“杭电杯”中国大学生算法设计超级联赛(7)

有n个点,每个点有对应的权重,1,2,3号点两两相连,从第四号点开始,给出两个点,这两点这之间存在边,将该点和这两个点进行连接,你需要选择一些点,这些点两两之间不存在边,且剩下的点构成一个无环的无向图
我们先考虑开始的三角形,如果三个点都选上或者任选两个点,那就违背了我们说的两两不存在边,但如果都不选,那么剩下的图一定有环,所以我们必须在三个点中选择一个点,我们枚举这个情况并确定二分图,最后遍历得到答案即可

tags:二分图

AC代码

/*****************@description:for the Escape Project*@author: Nebula_xuan* @Date: 2022-08-10 16:37:46*************************************************************************/#include <iostream>#include <algorithm>#include <cstring>#include <cstdio>#include <set>#include <vector>#include <queue>#include <map>#include <cmath>using namespace std;#define xx first#define yy second#define rep(i, h, t) for (int i = h; i <= t; i++)#define dep(i, t, h) for (int i = t; i >= h; i--)#define endl char(10)#define int long long//记得%d应该写成%lldtypedef pair<int, int> PII;typedef long long ll;const int N = 1e5 + 10;int a[N];signed main(){ios::sync_with_stdio(false);int t;cin >> t;while (t--){int n;cin >> n;vector<PII> e(n + 1);int ans = 0;for (int i = 1; i <= n; i++)cin >> a[i];for (int i = 4; i <= n; i++)cin >> e[i].xx >> e[i].yy;for (int i = 1; i <= 3; i++){vector<int> c(n + 1);c[i] = 1;int res = a[i];for (int j = 4; j <= n; j++){int u = e[j].xx, v = e[j].yy;if (c[u] == c[v] && c[u] == 0){c[j] = 1;res += a[j];}}ans = max(ans, res);}cout << ans << endl;}}

1003 Counting Stickmen

**Time Limit: 18000/9000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 871 Accepted Submission(s): 283
**

Problem Description

Namesolo has fallen in love with the game Stick Fight. But when he is playing the game, he wonders how to find out the number of stickmen in the game.

In this question, we define the stick man as shown in the picture above. It has a chain of length 1 as the head, two chains of length 2 as the arms, a chain of length 1 as the body, and two chains of length 1 as the legs. For example, the red part in this picture can be viewed as a stick man, with chain (2,3) to be the head, chains (3,4,6) and (3,9,10) to be the arms, chain (3,5) to be the body, and chains (5,7) and (5,8) to be the legs.

The game can be viewed as a tree, and Namesolo wants to know how many stickmen are there. Please note that two stickmen are different when there is at least one different edge between the two edge sets that make up the two stickmen.

Because the answer may be too large, Namesolo wants to know the answer modulo 998244353.

Input

The first line of input contains one integer T (1≤T≤15), indicating the number of test cases.

For each test case, the first line contains an integer n (1≤n≤5×105), indicating the number of vertices in the tree. Each of the following n−1 lines contains two integers a,b (1≤a,b≤n), indicating that there is an edge connecting a and b in the tree.

It is guaranteed that the sum of n over all cases won’t exceed 3×106.

Output

For each test case, output an integer representing the answer modulo 998244353.

Sample Input

1
9
1 2
2 3
3 4
2 5
5 6
2 7
7 8
7 9

Sample Output

1

Source

2022“杭电杯”中国大学生算法设计超级联赛(7)

给你一个图,问你有多少个火柴人,其中火柴人的定义是头的长度为1,手的长度为2,身体的长度为1,腿的长度为2
这里我们以“脖子”为核心,然后找子节点的度数即可
![[Pasted image 20220810213358.png]]
对于火柴人的手来说,所有度数大于等于2的子节点都能当作手用
对于度数大于等于3的子节点,都可以当作身体,所以我们还要记录一下子节点的度数,因为脚只要选两条即可,即Cn2C_{n}^{2}Cn2​种方案,其中n表示子节点的子节点的个数
但是要注意当把身体当作手来用的时候,这样是不合法的,删去即可

AC代码

/*****************@description:for the Escape Project*@author: Nebula_xuan* @Date: 2022-08-10 21:40:00*************************************************************************/#include <iostream>#include <algorithm>#include <cstring>#include <cstdio>#include <set>#include <vector>#include <queue>#include <map>#include <cmath>using namespace std;#define xx first#define yy second#define rep(i, h, t) for (int i = h; i <= t; i++)#define dep(i, t, h) for (int i = t; i >= h; i--)#define endl char(10)#define int long long//记得%d应该写成%lldtypedef pair<int, int> PII;typedef long long ll;const int N = 2e6 + 10;int e[N], ne[N], idx, h[N], d[N], hand[N], body[N];const int MOD = 998244353;int ans = 0;void add(int a, int b){e[idx] = b, ne[idx] = h[a], h[a] = idx++;}int work(int n){return n * (n - 1) / 2;}void dfs(int u, int fa){int cnth = 0, cntb = 0;int temp = 0;for (int i = h[u]; ~i; i = ne[i]){int v = e[i];cnth += hand[v];cntb += body[v];if (v == fa)continue;dfs(v, u);}if (d[u] <= 3)return;for (int i = h[u]; ~i; i = ne[i]){int v = e[i];temp += ((d[u] - 3) * body[v] % MOD) * ((work(cnth - hand[v]) - (cntb - body[v])) % MOD)%MOD;temp %= MOD;}ans += temp;ans %= MOD;}signed main(){ios::sync_with_stdio(false);int t;cin >> t;while (t--){ans = 0;idx = 0;int n;cin >> n;for (int i = 0; i <= 2 * n + 100; i++){e[i] = ne[i] = hand[i] = body[i] = 0;h[i] = -1;d[i] = 0;}for (int i = 1; i < n; i++){int a, b;cin >> a >> b;d[a]++, d[b]++;add(a, b);add(b, a);}for (int i = 1; i <= n; i++){hand[i] = d[i] - 1;if (d[i] >= 3)body[i] = work(d[i] - 1);}dfs(1, 0);cout << ans << endl;}}

2022“杭电杯”中国大学生算法设计超级联赛(7) 2022杭电多校第七场相关推荐

  1. 2022“杭电杯”中国大学生算法设计超级联赛 (1) 杭电多校第一场 2 3 4 5 8 12

    题目 1002 Dragon slayer 标程 1003 Backpack AC代码 1004 Ball AC代码 1008 Path AC代码 1009 Laser AC代码 1012 Alice ...

  2. 2022“杭电杯”中国大学生算法设计超级联赛 (2) 杭电多校第二场

    题目 1001 Static Query on Tree AC代码 1002 C++ to Python AC代码 1003 Copy AC代码 1005 Slayers Come AC代码 1007 ...

  3. 2022“杭电杯”中国大学生算法设计超级联赛(8)

    2022"杭电杯"中国大学生算法设计超级联赛(8) [题目链接](Search Result (hdu.edu.cn)) D Quel'Thalas 题目大意 在二维平面上,[0, ...

  4. 2022“杭电杯”中国大学生算法设计超级联赛(4)

    2022"杭电杯"中国大学生算法设计超级联赛(4) [题目链接](Search Result (hdu.edu.cn)) D Link with Equilateral Trian ...

  5. 2022“杭电杯”中国大学生算法设计超级联赛(8)题解报告

    Problem D. Quel'Thalas 题解: 手玩一下就会发现答案是就是2n. 代码: #include<iostream> #include<algorithm> # ...

  6. 2022“杭电杯”中国大学生算法设计超级联赛(6)题解报告

    1006.Maex 题意: 给定一棵由从 1 到 n 编号的 n 个顶点组成的有根树,根为顶点 1.顶点 i 有一个自然数权重 ai,并且没有两个不同的顶点具有相同的权重.bu是以u为根的子树的权重的 ...

  7. 2022“杭电杯”中国大学生算法设计超级联赛(2)1003.Copy

    样例输入: 1 5 3 1 2 3 4 5 2 4 1 2 4 2 5 样例输出: 6  题意: 有q次操作,有以下两种: 1.选择区间[l,r],复制[l,r]追加到r后,总的数组的大小(n)不变 ...

  8. 2022“杭电杯”中国大学生算法设计超级联赛(8)补题

    1004  Quel'Thalas 题意 在二维平面上,点在坐标在[0,n]的区间内,通过添加无限长的直线可以覆盖这些点,在不覆盖(0,0)的情况下要添加多少根直线. 思路 在两个方向上添加直线,所以 ...

  9. 2022“杭电杯”中国大学生算法设计超级联赛(8)补题 1011 (持续更新中)

    目录 1011 Stormwind(贪心--分割矩形) 1011 Stormwind(贪心–分割矩形) /*题解:想要切割的次数最多,让原矩形的一边尽可能被多次切割,另一边紧跟即可*/ #includ ...

  10. 2022“杭电杯”中国大学生算法设计超级联赛(1)1003 Backpack个人题解

    Backpack 题目描述 有n件物品,他们各自都有体积vi和价值wi,给你一个体积为m的背包,求是否能从这n件物品中取出若干件,使得它们的体积之和恰好为m,所有物品的异或和最大,最大值是多少 分析 ...

最新文章

  1. c++设置单元格填充色_更改数据后单元格自动填充颜色,从此以后再也不用核对数据了...
  2. C#-获取磁盘,cpu,内存信息
  3. 005_MySQL数据类型
  4. 安装pyecharts
  5. matlab二元一次方程求解_高中化学二元混合物的十字交叉法解法
  6. fork vfork exit _exit (转)
  7. linux下如何获取cpu的利用率
  8. C|C++中的静态全局变量,静态局部变量,全局变量,局部变量的区别
  9. 智能+大数据 在云+互联时代下创造的精准营销
  10. 显卡和CPU的关系像“主仆”,GPU的工作原理是什么
  11. cad两直线相交画圆弧_CAD两直线,如何用圆弧连接?
  12. win10后端开发环境搭建
  13. Linux日志怎么分析
  14. 想让人瞬间死心只能豁出去把自己和一种恶心的东西连接在一起,一旦生理反抗达成。想不死心也不行。
  15. Django3.0+Python3.8+MySQL8.0 个人博客搭建三|创建博客项目
  16. vb.NET入门总结
  17. 《c语言项目》学生成绩管理系统(devc++)
  18. 一、Glade-3安装配置
  19. Arduino Nano做NB-IoT透传项目
  20. 运筹说 第28期|论文速读之环境经济学中的影子价格

热门文章

  1. 计算机cpu好坏之分,学查看CPU天梯图,正确判断CPU性能好坏
  2. 计算机管理无效,win7右键菜单管理无效恢复方法
  3. R语言 伯努利试验和二项分布
  4. 塔尔萨大学计算机科学专业,塔尔萨大学专业
  5. NodeJS启动vue项目的坑
  6. IAR for STM8的简介、下载、安装及注册教程
  7. 解谜游戏 | 感受算法的魅力
  8. 由于找不到MSVCR100.dll,无法继续执行代码解决方法
  9. 如何申请成为企业微信,并成为第三方服务商
  10. hp服务器3c认证证书,戴尔 Dell PowerEdge R720 服务器3C认证证书,节能认证证书