HDU暑假多校第八场G-Card Game
一、题意
给出N个卡牌,卡牌的正反两面具有两个数字,取值范围为[1,2*n],给出若干个默认正面向上的卡牌,求最小反转多少张卡牌可以使得,每张卡牌朝上的面上都有一个不同的数字,同时满足最小反转次数的反转方法有多少个?
Alice and Bob are playing a card game. In this card game, a number is written on each face of the playing card. The rule of the game is described as follows:
- Alice arranges the cards in a row, and for each of the cards, she chooses one of its faces to place it up;
- Bob turns over minimum number of cards to make all numbers on the front faces unique.
They play the game some times, and Bob always succeeds making the numbers unique. However, both of them are not sure whether the number of cards flipped is minimum. Moreover, they want to figure out the number of different ways of turning over minimum number of cards to make the numbers unique. Two ways are considered equal if and only if the sets of flipped cards are equal. Please write a program to help them!
二、题解
考虑每张卡牌都有两个数字,要求必须选一个数字,则实际上可以考虑将卡牌表示成两个有向边——具有不同权重的边,权重表示反转的代价。具体来说,如果建立了边之后,对于每个联通快,如果有n个节点,且具有n-1或n个边,则可以给每条边分配一个节点——物理意义可以表示为给每张卡牌分配一个数字。对于其他情况则直接是不合法的。
考虑,n节点,n条边实际上是基环树,则分配时,可选的范围只有环的旋转方向(2个情况)。对于树结构,则可以自由地选择放弃使用哪个数字(n个情况)。
考虑暴力计算树DP统计以某个点为根的子树的权值和,实际上是一个O(N)的算法,则,直接求解复杂度为O(N2),不可接受。
考虑子树的定义,如果求出了父节点的DP值,且当前的DP值不包含父节点,则可以发现一个简单的状态转移方程:DP[NOW] = DP[FATHER] - EDGE(FATHER,NOW) - DP[NOW] + DP[CHILD] + EDGE[NOW,CHILD];这样做两遍遍历即可求出来我们要求的,以给定节点为根节点的权重值和,复杂度O(N)。
#include<bits/stdc++.h>
using namespace std; #define ll long long
const int MAXN = ;
const int MOD = ;
#define pp pair<int,int>
#define veci vector<int>
#define vecp vector<pp> int fa[MAXN],vis[MAXN],dp[MAXN];
vecp G[MAXN];
int n;
ll ans_cntt,ans_step,minn_cntt,minn_step; int cnt_e,cnt_v; void init_dfs(int now)
{
vis[now] = ;
int len = G[now].size();
for(int i=;i<len;++i)
{
int tar = G[now][i].first;
if(!vis[tar])init_dfs(tar);
}
cnt_v ++;
cnt_e += len;
} void tree_dp(int now,int father){
fa[now] = father;
int len = G[now].size();
dp[now] = ;
for(int i=;i<len;++i){
int tar = G[now][i].first;
int val = G[now][i].second;
if(father == tar)continue;
tree_dp(tar,now);
dp[now] += val + dp[tar];
}
} void dp_dfs(int now){
if(fa[now] == ){
if(minn_step > dp[now]){
minn_step = dp[now];
minn_cntt =;
}else if(minn_step == dp[now])minn_cntt ++; int len = G[now].size();
for(int i=;i<len;++i){
int tar = G[now][i].first;
dp_dfs(tar);
}
}else{
dp[now] = -dp[now];
int len = G[now].size();
for(int i=;i<len;++i){
int tar =G[now][i].first;
int val = G[now][i].second;
dp[now] += dp[tar] + val;
if(tar == fa[now])dp[now] -= !val;
}
if(minn_step > dp[now]){
minn_step = dp[now];
minn_cntt =;
}else if(minn_step == dp[now])minn_cntt ++; for(int i=;i<len;++i){
int tar =G[now][i].first;
if(tar == fa[now])continue;
dp_dfs(tar);
}
}
} veci cir_edge;
pp cir_vector;
int cir_val; bool find_circle(int now,int last){
vis[now] = ;
int len = G[now].size();
int cnt_last = ;
for(int i=;i<len;++i){
int tar = G[now][i].first;
int val = G[now][i].second;
if(tar == last && !cnt_last){
cnt_last++;
continue;
}
if(vis[tar] == ){
cir_vector = make_pair(now,tar);
cir_val = !val;
return true;
}
if(find_circle(tar,now))return true;
}return false;
} bool deal_circle(int now,int last,int target,int last_val){
int len =G[now].size();
int cnt_last = ;
int next = ;
for(int i=;i<len;++i){
int tar =G[now][i].first;
int val = G[now][i].second;
if(tar == last && cnt_last == && val == !last_val){
cnt_last++;
continue;
}
if(tar == target){
cir_edge.push_back(val);
next = tar;
break;
}
if(deal_circle(tar,now,target,val)){
cir_edge.push_back(val);
next = tar;
break;
}
}
if(next == )return false; for(int i=;i<len;++i){
int tar = G[now][i].first;
int val = G[now][i].second;
if(tar == last || tar == next ||tar == now)continue;
tree_dp(tar,now);
minn_step += dp[tar] + val;
}
return true;
} void init(){
memset(vis,,sizeof(vis));
for(int i=;i<*n+;++i)G[i].clear(); int succ = ; for(int i=;i<n;++i){
int a,b;
cin>>a>>b;
G[a].push_back(make_pair(b,));
G[b].push_back(make_pair(a,));
}
ans_cntt = ;
ans_step = ;
for(int i=;i<=*n;++i){
if(G[i].empty())continue;
if(vis[i])continue;
cnt_e = ;
cnt_v = ;
init_dfs(i);
cnt_e/=; // cout<<"check_cnt: "<<cnt_e<<" "<<cnt_v<<endl; if(cnt_e == cnt_v-){
minn_step = INT_MAX;
minn_cntt = ;
tree_dp(i,);
dp_dfs(i); ans_step += minn_step;
ans_cntt *= minn_cntt;
ans_cntt %=MOD;
continue;
}
if(cnt_e == cnt_v){
minn_step = ;
cir_edge.clear();
find_circle(i,);
deal_circle(cir_vector.first,cir_vector.second,cir_vector.first,cir_val);
int tmp1 = ,tmp2 = ;
int len = cir_edge.size();
for(int i=;i<len;++i){
tmp1 += cir_edge[i];
tmp2 += !cir_edge[i];
}
// cout<<"check_tmp: "<<tmp1<<" "<<tmp2<<endl;
ans_step += minn_step + min(tmp1,tmp2);
if(tmp1 == tmp2 )ans_cntt *= ;
ans_cntt %= MOD; continue;
}
succ = ;
break;
} if(succ){
cout<<ans_step<<" "<<ans_cntt<<"\n";
}else{
cout<<"-1 -1\n";
}
} int main(){ cin.sync_with_stdio(false);
int t;
cin>>t;
while(cin>>n)init(); return ;
}
HDU暑假多校第八场G-Card Game的更多相关文章
- HDU暑假多校第八场J-Taotao Picks Apples
一.题意 给定一个序列,之后给出若干个修改,修改的内容为在原序列的基础上,将某一位元素的值改成给定的值<每次修改相互独立,不保存修改后的结果>.之后询问,在选择第一位元素的情况下,最长递增 ...
- HDU暑假多校第三场H.Monster Hunter
一.题意 给定一个树状地图,每个树节点上有一只怪物,打死一只怪物的过程中将会消耗A点HP,打死之后将会获得B点HP.因为树状结构,所以每只怪物必须先打死父节点的怪兽之后在打死子节点的怪物.现在,给定每 ...
- HDU暑假多校第六场K-werewolf
一.题意 好人必然说真话,坏人不一定说真话,给定N个人的言论<每人一个发言.不谈及自己>,要求指出有多少个人一定是好人,有多少个人一定是坏人.#define 狼人 坏人#define 村民 ...
- HDU暑假多校第四场J-Let Sudoku Rotate
一.题意 Sudoku is a logic-based, combinatorial number-placement puzzle, which is popular around the wor ...
- 牛客多校第八场 G Gemstones 栈/贪心
题意: 对于一个序列,把可以把连着三个相同的字母拿走,问最多拿走多少组. 题解: 直接模拟栈,三个栈顶元素相同则答案+1,并弹出栈 #include<bits/stdc++.h> usin ...
- 2020牛客暑假多校训练营 第二场 G Greater and Greater bitset
LINK:Greater and Greater 确实没能想到做法. 考虑利用bitset解决问题. 做法是:逐位判断每一位是否合法 第一位 就是 bitset上所有大于\(b_1\)的位置 置为1. ...
- [HDU6304][数学] Chiaki Sequence Revisited-杭电多校2018第一场G
[HDU6304][数学] Chiaki Sequence Revisited -杭电多校2018第一场G 题目描述 现在抛给你一个数列\(A\) \[ a_n=\begin{cases}1 & ...
- 牛客多校第三场 G Removing Stones(分治+线段树)
牛客多校第三场 G Removing Stones(分治+线段树) 题意: 给你n个数,问你有多少个长度不小于2的连续子序列,使得其中最大元素不大于所有元素和的一半 题解: 分治+线段树 线段树维护最 ...
- 2019牛客多校第八场 F题 Flowers 计算几何+线段树
2019牛客多校第八场 F题 Flowers 先枚举出三角形内部的点D. 下面所说的旋转没有指明逆时针还是顺时针则是指逆时针旋转. 固定内部点的答案的获取 anti(A)anti(A)anti(A)或 ...
随机推荐
- python26 re正则表达式
#coding:utf-8 #/usr/bin/python """ 2018-11-25 dinghanhua re """ import ...
- 【安卓】imageView.scaleType取centerCrop后,再用padding时显示异常?
imgView.setCropToPadding(true);就可以
- springboot(服务端接口)获取URL请求参数的几种方法
原文地址:http://www.cnblogs.com/xiaoxi/p/5695783.html 一.下面为7种服务端获取前端传过来的参数的方法 常用的方法为:@RequestParam和@Req ...
- mac下配置Node.js开发环境、express安装、创建项目
mac下配置Node.js开发环境.express安装.创建项目 一.node.js的安装 去官网下载对应的平台版本就可以了,https://nodejs.org 二.express安装 sudo n ...
- java 线程状态图
- CodeForces - 598C Nearest vectors(高精度几何 排序然后枚举)
传送门: http://codeforces.com/problemset/problem/598/C Nearest vectors time limit per test 2 seconds me ...
- Hive中使用LZO
hive 中使用lzo 1 启动hive 错误Exception in thread "main" java.lang.NoClassDefFoundError: org/apac ...
- An error occurred during the installation of assembly 'Microsoft.VC90.ATL or 'Microsoft.VC80.ATL'
An error occurred during the installation of assembly 'Microsoft.VC90.ATL or 'Microsoft.VC80.ATL' 下载 ...
- vue2.0移除或更改的一些东西
一.vue2.0移除了$index和$key 虽然说现在很多文章说他们的代码是vue2.0版本的,但是有一些仔细一看,发现并不全是2.0版本,有些语法还是1.0的版本,比如这个$index,$key, ...
- center os 文件读写权限
五.使用chmod和数字改变文件或目录的访问权限文件和目录的权限表示,是用rwx这三个字符来代表所有者.用户组和其他用户的权限.有时候,字符似乎过于麻烦,因此还有另外一种方法是以数字来表示权限,而且仅 ...