2015-2016 ACM-ICPC Northeastern European Regional Contest (NEERC 15)C - Cactus Jubilee
题意:给一颗仙人掌,要求移动一条边,不能放在原处,移动之后还是一颗仙人掌的方案数(仙人掌:无向图,每条边只在一个环中),等价于先删除一条边,然后加一条边
题解:对于一颗仙人掌,分成两种边,1:环边:环上的边2,树边:非环上的边
考虑1.删除树边,那么只需联通两个联通快,那么方案数就是两个联通块乘积-1(除去删除的边)
2.删除环边,那么我们假设删除所有环,那么图变成了深林,方案数就是深林中每棵树任意两点连接,方案数就是全部的和,先维护一个每个环上的点有多少树边,对于每个树边联通块(大小x)共贡献是x*(x-1)/2-(x-1),对于每个环,我们先算出所有答案,按个减去每个环上点的贡献,然后考虑删除环边之后总树边联通块的贡献
bcc维护树边,并查集维护树边联通块
//#pragma GCC optimize(2)
//#pragma GCC optimize(3)
//#pragma GCC optimize(4)
//#pragma GCC optimize("unroll-loops")
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<bits/stdc++.h>
#define fi first
#define se second
#define db double
#define mp make_pair
#define pb push_back
#define pi acos(-1.0)
#define ll long long
#define vi vector<int>
#define mod 1000000007
#define ld long double
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#define pll pair<ll,ll>
#define pil pair<int,ll>
#define pli pair<ll,int>
#define pii pair<int,int>
//#define cd complex<double>
#define ull unsigned long long
//#define base 1000000000000000000
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
#define fin freopen("a.txt","r",stdin)
#define fout freopen("a.txt","w",stdout)
#define fio ios::sync_with_stdio(false);cin.tie(0)
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline void sub(ll &a,ll b){a-=b;if(a<0)a+=mod;}
inline void add(ll &a,ll b){a+=b;if(a>=mod)a-=mod;}
template<typename T>inline T const& MAX(T const &a,T const &b){return a>b?a:b;}
template<typename T>inline T const& MIN(T const &a,T const &b){return a<b?a:b;}
inline ll qp(ll a,ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;a=a*a%mod,b>>=1;}return ans;}
inline ll qp(ll a,ll b,ll c){ll ans=1;while(b){if(b&1)ans=ans*a%c;a=a*a%c,b>>=1;}return ans;}
using namespace std;
const double eps=1e-8;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int N=100000+100,maxn=50000+10,inf=0x3f3f3f3f;
int n,m;
vi v[N];
int dfn[N],low[N],ind,pre[N],fa[N];
ll ans,sum,sz[N],tr[N];
map<pii,bool>tree;
int Find(int x){return fa[x]==x?x:fa[x]=Find(fa[x]);}
void tarjan(int u,int f)
{
sz[u]=1;
dfn[u]=low[u]=++ind;
for(int x:v[u])
{
if(x==f)continue;
if(!dfn[x])
{
tarjan(x,u);
sz[u]+=sz[x];
low[u]=min(low[u],low[x]);
if(low[x]>dfn[u])tree[mp(u,x)]=tree[mp(x,u)]=1,ans+=sz[x]*(n-sz[x])-1;
}
else if(dfn[x]<dfn[u])low[u]=min(low[u],dfn[x]);
}
}
void dfs(int u,int f)
{
dfn[u]=1;
for(int x:v[u])
{
if(x==f)continue;
if(!dfn[x]&&tree.find(mp(u,x))!=tree.end())//tree edge
{
dfs(x,u);
int fx=Find(u),fy=Find(x);
if(fx!=fy)fa[fx]=fy,tr[fy]+=tr[fx];
}
}
}
void dfs1(int u,int f)
{
dfn[u]=1;
for(int x:v[u])
{
if(x==f)continue;
if(!dfn[x])pre[x]=u,dfs1(x,u);
else if(dfn[x]==1)
{
ll res=sum,co=0,p=0;
for(int now=u;now!=x;now=pre[now])
{
int fx=Find(now);
co+=tr[fx],p++,res-=1ll*(tr[fx]-1)*tr[fx]/2-(tr[fx]-1);
}
int fx=Find(x);
co+=tr[fx],p++,res-=1ll*(tr[fx]-1)*tr[fx]/2-(tr[fx]-1);
res=(res+1ll*(co-1)*co/2-(co-1)-1)*p;
// printf("%d %d %lld %lld %lld***\n",u,x,co,p,res);
ans+=res;
}
}
dfn[u]=2;
}
int main()
{
freopen("cactus.in","r",stdin);
freopen("cactus.out","w",stdout);
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)fa[i]=i,tr[i]=1;
for(int i=0;i<m;i++)
{
int x,k,last=0;scanf("%d",&k);
while(k--)
{
scanf("%d",&x);
if(last)v[last].pb(x),v[x].pb(last);
last=x;
}
}
tarjan(1,-1);
// printf("%lld\n",ans);
memset(dfn,0,sizeof dfn);
for(int i=1;i<=n;i++)if(!dfn[i])dfs(i,-1);
for(int i=1;i<=n;i++)
{
if(fa[i]==i)
{
sum+=1ll*(tr[i]-1)*tr[i]/2-(tr[i]-1);
// printf("%d %d\n",i,tr[i]);
}
}
memset(dfn,0,sizeof dfn);
dfs1(1,-1);
printf("%lld\n",ans);
return 0;
}
/********************
9 4
5 1 2 3 4 5
4 2 8 7 4
2 6 7
2 8 9
********************/
2015-2016 ACM-ICPC Northeastern European Regional Contest (NEERC 15)C - Cactus Jubilee的更多相关文章
- 2015-2016 ACM-ICPC Northeastern European Regional Contest (NEERC 15)
NEERC 15 题解1 题解2 官方题解
- 2017-2018 ACM-ICPC Northern Eurasia (Northeastern European Regional) Contest (NEERC 17)
2017-2018 ACM-ICPC Northern Eurasia (Northeastern European Regional) Contest (NEERC 17) A 题意:有 n 个时刻 ...
- Editing 2011-2012 ACM-ICPC Northeastern European Regional Contest (NEERC 11)
NEERC 11 *wiki链接[[https://acm.ecnu.edu.cn/wiki/index.php?title=2011-2012_ACM-ICPC_Northeastern_Europ ...
- 2012-2013 ACM-ICPC Northeastern European Regional Contest (NEERC 12)
Problems # Name A Addictive Bubbles1 addictive.in / addictive.out 2 s, 256 MB x438 B Blin ...
- 2017-2018 ACM-ICPC Northern Eurasia (Northeastern European Regional) Contest (NEERC 17) 日常训练
A - Archery Tournament 题目大意:按时间顺序出现靶子和射击一个位置,靶子的圆心为(x, y)半径为r,即圆与x轴相切,靶子不会重叠,靶子被击中后消失, 每次射击找出哪个靶子被射中 ...
- 2002-2003 ACM-ICPC Northeastern European Regional Contest (NEERC 02) H Heroes Of Might And Magic (隐含dp)
问题是求一个方案,实际隐含一个dp.法力是递减的,所以状态是DAG,对于一个确定的状态,我们贪心地希望英雄的血量尽量大. 分析:定义状态dp[i][p][h]表示是已经用了i的法力值,怪兽的位置在p, ...
- 2002-2003 ACM-ICPC Northeastern European Regional Contest (NEERC 02) A Amusing Numbers (数学)
其实挺简单的.先直接算出之前已经排在k这个数前面的数字.比如543是三位的,那么100~543都是可以的,两位的10~54. 如果还需要往前面补的话,那么依次考虑1000~5430,5430是上界不能 ...
- 2002-2003 ACM-ICPC Northeastern European Regional Contest (NEERC 02)
B Bricks 计算几何乱搞 题意: 给你个立方体,问你能不能放进一个管道里面. 题解: 这是一道非常迷的题,其问题在于,你可以不正着放下去,你需要斜着放.此时你需要枚举你旋转的角度,来判断是否可行 ...
- ACM ICPC 2010–2011, Northeastern European Regional Contest St Petersburg – Barnaul – Tashkent – Tbilisi, November 24, 2010
ACM ICPC 2010–2011, Northeastern European Regional Contest St Petersburg – Barnaul – Tashkent – Tbil ...
随机推荐
- P3338 [ZJOI2014]力
思路 颓柿子的题目 要求求这样的一个式子 \[ F_j=\sum_{i<j}\frac{q_iq_j}{(i-j)^2}-\sum_{i>j}\frac{q_iq_j}{(i-j)^2} ...
- shiro 前后端分离 seseeionId 问题
http://www.cnblogs.com/cshhs/p/9269411.html https://www.w3cschool.cn/shiro/rmvk1if1.html http://www. ...
- (转) RNN models for image generation
RNN models for image generation MARCH 3, 2017 Today we’re looking at the remaining papers from the ...
- github 首页不显示提交记录
原因,一般是因为提交登录里配置的邮箱不是 github 上记录的邮箱. 如何查询提交记录里的邮箱? 如果是本地仓库, 使用小乌龟什么的定位到要查看的提交就可以了.如果是远程仓库, 进入提交记录详情,在 ...
- 3、Python迭代器、列表解析及生成器(0530)
1.动态语言 sys.getrefcount() //查看对象的引用计数 增加对象的引用计数场景 对象创建时:以赋值的方式,创建变量名的同时就会创建变量 将对象添加进容器时:类似list.app ...
- Python 爬虫常用的库
一.常用库 1.requests 做请求的时候用到. requests.get("url") 2.selenium 自动化会用到. 3.lxml 4.beautifulsoup 5 ...
- Addition Chains
题目描述: 一个与 n 有关的整数加成序列 < a0 , a1 , a2 ...am> 满足一下四个条件: 1.a0=1 2.am=n 3.a0<a1<a2<...< ...
- phpredis基本操作
字符串,用于存储变动少的信息 创建对象 $red = Red::create(); 设置值 $red->set('name','张三'); 设置有效期 $red->set('name',' ...
- flutter安装与配置 v1.2.1版本
1---- 上面是下载地址https://flutter.dev/docs/development/tools/sdk/archive#windows 2---- 下载后,解压安装到C盘 3--- 测 ...
- leecode第八十九题(格雷编码)
class Solution { public: vector<int> grayCode(int n) { vector<int> res; res.push_back(); ...