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)或 ...
随机推荐
- Struts的学习-例子
一.新建空项目user和配置maven实现下面的页面 1.配置内容 2.编写struts.xml实现页面 <!--定义一个useraction--> <package name=&q ...
- 7 - py面向对象一条龙服务
Python从设计之初就已经是一门面向对象的语言,在python里所有东西皆是对象. 下面通过一个实例来说明什么是面向对象. 引子 你是一家公司的员工,公司现在要开发一款“人狗战争”的游戏,人狗战争肯 ...
- February 27 2017 Week 9 Monday
All the bright precious things fade so fast. 所有的光鲜靓丽都敌不过时间. Try to make some things endurable and et ...
- 原生JavaScript实现JSON合并(递归深度合并)
// 遇到相同元素级属性,以(minor)为准 // 不返还新Object,而是main改变 function mergeJSON(minor, main) { for(var key in mino ...
- IOS ASI和AFN的 区别
一.底层实现 1> AFN的底层基于OC的NSURLConnection和NSURLSession2> ASI的底层基于纯C语言的CFNetwork框架3> ASI的运行性能 高于 ...
- Python语言程序设计基础(6)—— 组合数据类型
tuple 元组(创建后不能修改) tuple = "cat","dog","tiger","human" print( ...
- Codeforces Round #333 (Div. 1)
A. The Two Routes In Absurdistan, there are n towns (numbered 1 through n) and m bidirectional railw ...
- 2018.12.2 Mac环境下mysql图形化界面的Navicat premium 12 中文版安装与激活
软件链接: https://pan.baidu.com/s/1ZUNLQ1DW9rQZUzDXQn2rWQ 提取码: 8i78 复制这段内容后打开百度网盘手机App,操作更方便哦 注意最新版 12.0 ...
- VI编辑器查找替换
1.Vi下进行查找 VI命令模式下:输入“/要查找的词”回车就会进入查找,你可以按“n”查找下一个,按“N”查找上一个.类似查找命令“?”与“/”的区别是“/”为向下查找,“?”为向上查找. 2.Vi ...
- 64 位系统(win7/win8) 下使用C# 程序问题
1 C# 程序是控制台类,使用的组件如果是32位,建议在编译的时候,platform (X86,AnyCPU,X64)选择X86 .使用X86 模式编译,才能调用32位程序的API. 2 ASP. ...