[AGC006F]Blackout-三染色

Blackout

Problem Statement

We have a grid with N rows and N columns. The cell at the i-th row and j-th column is denoted (i, j).

Initially, M of the cells are painted black, and all other cells are white. Specifically, the cells (a1, b1), (a2, b2), …, (aM, bM) are painted black.

Snuke will try to paint as many white cells black as possible, according to the following rule:

If two cells (x, y) and (y, z) are both black and a cell (z, x) is white for integers 1≤x,y,z≤N, paint the cell (z, x) black.
Find the number of black cells when no more white cells can be painted black.

Constraints

1≤N≤105
1≤M≤105
1≤ai,bi≤N
All pairs (ai, bi) are distinct.

Input

The input is given from Standard Input in the following format:

N M
a1 b1
a2 b2
:
aM bM

Output

Print the number of black cells when no more white cells can be painted black.

Sample Input 1

3 2
1 2
2 3

Sample Output 1

3

It is possible to paint one white cell black, as follows:

Since cells (1, 2) and (2, 3) are both black and a cell (3, 1) is white, paint the cell (3, 1) black.

Sample Input 2

2 2
1 1
1 2

Sample Output 2

4

It is possible to paint two white cells black, as follows:

Since cells (1, 1) and (1, 2) are both black and a cell (2, 1) is white, paint the cell (2, 1) black.
Since cells (2, 1) and (1, 2) are both black and a cell (2, 2) is white, paint the cell (2, 2) black.

Sample Input 3

4 3
1 2
1 3
4 4

Sample Output 3

3

No white cells can be painted black.


F题没有E题难系列……
思路很巧妙的一道题……


思路:
考虑这个矩阵甚至无法构建的事实,把方格(i,j)想象为一条边i->j。
那么对于输入,可以得到一张有向图。

考虑用三种颜色给原图每个联通块染色。
令这三种颜色分别为0、1、2,且0->1->2->0。

可以发现题目中的染色方法便是这张图上,若某个0->1且1->2,则产生2->0的边。
那么在染色后,如果染色成功了,那么当前联通块对答案的贡献便是:所有0向1连边,所有1向2连边,所有2向1连边,的总边数。
如果不成功但是用了每种颜色,画图可知一定是出现了环(包括自环),于是这个联通块可以被连成一个完全图。
如果不成功也没有使用每一种颜色,那么这个联通块的贡献就是输入的边。

于是做完了~

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>

using namespace std;

typedef long long ll;
const int N=5e5+9;

ll to[N<<1],nxt[N<<1],beg[N],tot;
ll n,m,c[N],w[N<<1];
ll ans,f[4],flag;

inline void add(int u,int v,int c)
{
    to[++tot]=v;
    nxt[tot]=beg[u];
    w[tot]=c;
    beg[u]=tot;
}

inline void dfs(int u)
{
    f[c[u]]++;
    for(int i=beg[u];i;i=nxt[i])
    {
        if(w[i]==1)f[3]++;
        if(!(~c[to[i]]))
        {
            c[to[i]]=(c[u]+w[i])%3;
            dfs(to[i]);
        }
        else if (c[to[i]]!=(c[u]+w[i])%3)
            flag=1;
    }
}

int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1,u,v;i<=m;i++)
    {
        scanf("%d%d",&u,&v);
        add(u,v,1);add(v,u,2);
    }

    memset(c,-1,sizeof(c));
    for(int i=1;i<=n;i++)
        if(!(~c[i]))
        {
            f[c[i]=0]=f[1]=f[2]=f[3]=flag=0;
            dfs(i);
            if(flag)
            {
                ll cnt=f[0]+f[1]+f[2];
                ans+=cnt*cnt;
                goto nxt;
            }
            if((!f[0]) || (!f[1]) || (!f[2]))
            {
                ans+=f[3];
                goto nxt;
            }

            ans+=f[0]*f[1];
            ans+=f[1]*f[2];
            ans+=f[2]*f[0];

            nxt:;
        }

    printf("%lld\n",ans);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值