一、题意

给出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的更多相关文章

  1. HDU暑假多校第八场J-Taotao Picks Apples

    一.题意 给定一个序列,之后给出若干个修改,修改的内容为在原序列的基础上,将某一位元素的值改成给定的值<每次修改相互独立,不保存修改后的结果>.之后询问,在选择第一位元素的情况下,最长递增 ...

  2. HDU暑假多校第三场H.Monster Hunter

    一.题意 给定一个树状地图,每个树节点上有一只怪物,打死一只怪物的过程中将会消耗A点HP,打死之后将会获得B点HP.因为树状结构,所以每只怪物必须先打死父节点的怪兽之后在打死子节点的怪物.现在,给定每 ...

  3. HDU暑假多校第六场K-werewolf

    一.题意 好人必然说真话,坏人不一定说真话,给定N个人的言论<每人一个发言.不谈及自己>,要求指出有多少个人一定是好人,有多少个人一定是坏人.#define 狼人 坏人#define 村民 ...

  4. HDU暑假多校第四场J-Let Sudoku Rotate

    一.题意 Sudoku is a logic-based, combinatorial number-placement puzzle, which is popular around the wor ...

  5. 牛客多校第八场 G Gemstones 栈/贪心

    题意: 对于一个序列,把可以把连着三个相同的字母拿走,问最多拿走多少组. 题解: 直接模拟栈,三个栈顶元素相同则答案+1,并弹出栈 #include<bits/stdc++.h> usin ...

  6. 2020牛客暑假多校训练营 第二场 G Greater and Greater bitset

    LINK:Greater and Greater 确实没能想到做法. 考虑利用bitset解决问题. 做法是:逐位判断每一位是否合法 第一位 就是 bitset上所有大于\(b_1\)的位置 置为1. ...

  7. [HDU6304][数学] Chiaki Sequence Revisited-杭电多校2018第一场G

    [HDU6304][数学] Chiaki Sequence Revisited -杭电多校2018第一场G 题目描述 现在抛给你一个数列\(A\) \[ a_n=\begin{cases}1 & ...

  8. 牛客多校第三场 G Removing Stones(分治+线段树)

    牛客多校第三场 G Removing Stones(分治+线段树) 题意: 给你n个数,问你有多少个长度不小于2的连续子序列,使得其中最大元素不大于所有元素和的一半 题解: 分治+线段树 线段树维护最 ...

  9. 2019牛客多校第八场 F题 Flowers 计算几何+线段树

    2019牛客多校第八场 F题 Flowers 先枚举出三角形内部的点D. 下面所说的旋转没有指明逆时针还是顺时针则是指逆时针旋转. 固定内部点的答案的获取 anti(A)anti(A)anti(A)或 ...

随机推荐

  1. appendChild与Transition动画

    在createElement之后,直接把这个div append到body中,是不会触发css3 transition动画的 必须要让浏览器计算div的css属性后,然后再设置div的style,才会 ...

  2. Android(java)学习笔记40:WindowManager 中LayoutParams的各种属性

    1. WindowManager 中LayoutParams的各种属性 WindowManager.LayoutParams 是 WindowManager 接口的嵌套类(内部类):它继承于 View ...

  3. 面向对象编程(OOP)、面向组件编程(COP)、面向方面编程(AOP)和面向服务编程(SOP)

    http://blog.csdn.net/hjf19790118/article/details/6919265 1.什么是面向对象编程(Object-Oriented Programming)? 面 ...

  4. POJ 最小球覆盖 模拟退火

    最小球覆盖:用半径最小的球去覆盖所有点. 纯粹的退火算法,是搞不定的,精度不够,不然就会TLE,根本跑不出答案来. 任取一点为球心,然后一点点靠近最远点.其实这才是最主要的. 因为:4个点确定一个球, ...

  5. 【转】 ios的手势操作之UIGestureRecognizer浅析

    一.概述 iPhone中处理触摸屏的操作,在3.2之前是主要使用的是由UIResponder而来的如下4种方式: - (void)touchesBegan:(NSSet *)touches withE ...

  6. 【转】Android Fragment 真正的完全解析(上)

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/37970961 自从Fragment出现,曾经有段时间,感觉大家谈什么都能跟Fra ...

  7. 2018.11.9 Dubbo入门学习

    1.什么是Dubbo dubbo.io 代表是开源的 DUBBO是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,是阿里巴巴SOA服务化治理方案的核心框架,每天为2,000+个服 ...

  8. 融云SDK:获取用户Token的方法

    融云SDK查看ServerAPI里面有个获取Token的方法,本以为只要传三个参数就可以.后来发现,在请求头有几个必须要传的参数,否则服务器返回401(未授权).拿获取Token接口为例子 如图所示, ...

  9. QS:vue中qs的使用

    关于Vue中,序列化字符串,处理发送请求的参数 使用工具qs来处理参数 步骤: 1.首先先下载: npm i qs 2.然后引入 : import qs from 'qs' 3.qs主要有两个方法 : ...

  10. WP | 后台PHP脚本无法修改及服务器无法直接写入问题的解决

    问题描述 试图修改页脚的时候,发现WordPress后台Appearance(外观)选项之下没有Editor(编辑)的子选项,之后进行设置修改后又发现无法保存. 试图安装插件,但是无法直接安装,推测是 ...