一、题意

给出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. 用python管理Cisco路由器

    目前DevOps是整个运维发展的方向,Network的运维也一样.使用程序控制底层的路由器是最基本的要求之一. 本文简单解释如何用Python控制路由器,对网络设备进行配置. Python和网络设备连 ...

  2. Android数字签名解析(一)

     一.数字签名概述 所谓"数字签名"就是通过某种password运算生成一系列符号及代码组成电子password进行签名,来取代书写签名或印章. 数字签名有两种功效:一是能确定消息 ...

  3. [19/03/22-星期五] 异常(Exception)(二)_捕获异常

    一.概念 捕获异常是通过3个关键词来实现的:try-catch-finally.用try来执行一段程序,如果出现异常,系统抛出一个异常,可以通过它的类型来捕捉(catch)并处理它, 最后一步是通过f ...

  4. js读取跨域webapi传送回来的cookie 要点

    1.webapi 返回cookie时,httpOnly=false 2.webapi 接收Origins 不能为* 3.js端 请求时,withCredentials必须: true   //`wit ...

  5. 移动端Vue回到顶部

    html: <div class="totop" id="totop" @click="Top" v-show="totop ...

  6. JavaScript js调用堆栈(二)

    本文主要介绍JavaScript的内存空间 var a = 20; var b = 'abc'; var c = true; var d = { m: 20 } 首先需要对栈(stack),堆(hea ...

  7. 重置按钮_reset

    function formreset(form){ for(var i=0;i<frmMain.length;i++){ if(frmMain.item(i).type=="text& ...

  8. 跨域-iframe

    跨域 同源策略:为保障用户信息安全,防止恶意网站窃取数据的一种安全策略. "同源":协议相同.域名相同.端口号相同 "非同源": 1.Cookie.LocalS ...

  9. 【TOJ 1449】Area of Circles II(求不同位置的两圆面积之和)

    描述 There are two circles on the plane. Now you must to calculate the area which they cover the plane ...

  10. leetcode笔记(七)529. Minesweeper

    题目描述 Let's play the minesweeper game (Wikipedia, online game)! You are given a 2D char matrix repres ...