题意:给一颗仙人掌,要求移动一条边,不能放在原处,移动之后还是一颗仙人掌的方案数(仙人掌:无向图,每条边只在一个环中),等价于先删除一条边,然后加一条边

题解:对于一颗仙人掌,分成两种边,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的更多相关文章

  1. 2015-2016 ACM-ICPC Northeastern European Regional Contest (NEERC 15)

    NEERC 15 题解1 题解2 官方题解

  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 个时刻 ...

  3. 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 ...

  4. 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 ...

  5. 2017-2018 ACM-ICPC Northern Eurasia (Northeastern European Regional) Contest (NEERC 17) 日常训练

    A - Archery Tournament 题目大意:按时间顺序出现靶子和射击一个位置,靶子的圆心为(x, y)半径为r,即圆与x轴相切,靶子不会重叠,靶子被击中后消失, 每次射击找出哪个靶子被射中 ...

  6. 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, ...

  7. 2002-2003 ACM-ICPC Northeastern European Regional Contest (NEERC 02) A Amusing Numbers (数学)

    其实挺简单的.先直接算出之前已经排在k这个数前面的数字.比如543是三位的,那么100~543都是可以的,两位的10~54. 如果还需要往前面补的话,那么依次考虑1000~5430,5430是上界不能 ...

  8. 2002-2003 ACM-ICPC Northeastern European Regional Contest (NEERC 02)

    B Bricks 计算几何乱搞 题意: 给你个立方体,问你能不能放进一个管道里面. 题解: 这是一道非常迷的题,其问题在于,你可以不正着放下去,你需要斜着放.此时你需要枚举你旋转的角度,来判断是否可行 ...

  9. 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 ...

随机推荐

  1. (转)JPA & Restful

    参考博客: JPA: https://www.jianshu.com/p/b6932740f3c0 https://shimo.im/docs/zOer2qMVEggbF33d/ Restful: h ...

  2. [CodeForces - 919B] Perfect Number

    题目链接:http://codeforces.com/problemset/problem/919/B AC代码: #include<cstdio> using namespace std ...

  3. 51nod 1672 区间交(贪心)

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1672 题意: 思路:其实这就是一个经典的区间贪心问题,只需要按照左端点排 ...

  4. 在js文件里调用另一个js文件里的函数

    这个是我今天解决的一个小问题,我在创建界面的时候,根据不同的界面需求对应创建了不同的js文件来搭建界面,搭建完毕之后再将各个生成页面的函数汇总到主界面上,通过visibility属性切换显示,这时候出 ...

  5. Easyui使用心得(1)--DateGrid表格

    最近一直在用easyui这个控件,有一点心得,在这里和大家分享一下,也是对自己工作的一个小小的总结,希望可以形成一个完整的Easyui的笔记体系,可以方便更多的人 因为自己也是在摸索中前进,难免有遗漏 ...

  6. Scrapy创建爬虫项目

    1.打开cmd命令行工具,输入scrapy startproject 项目名称 2.使用pycharm打开项目,查看项目目录 3.创建爬虫,打开CMD,cd命令进入到爬虫项目文件夹,输入scrapy ...

  7. Centos7安装配置Apache+PHP+Mysql+phpmyadmin

    转载自: Centos7安装配置Apache+PHP+Mysql+phpmyadmin 一.安装Apache yum install httpd 安装成功后,Apache操作命令: systemctl ...

  8. 6-1 建立客户端与zk服务端的连接

    6-1 建立客户端与zk服务端的连接 zookeeper原生java api使用 会话连接与恢复; 节点的增删改查; watch与acl的相关操作; 导入jar包;

  9. js_为元素动态注册事件

    <head> <title></title> <script type="text/javascript">//window.onl ...

  10. 学习笔记31—endnote设置

    修改文献引用设置: JournalArticle: Author. (Year). Title. [Translated Title]. [Reviewed Item]. Journal|, Volu ...