Educational Codeforces Round 46 (Rated for Div. 2)E. We Need More Bosses

题目链接:E. We Need More Bosses

题解:tarjan有向图缩点之后求树的直径就是答案:应为在同一个强联通里的边就不是必须边,参考了这个

#include<bits/stdc++.h>
#include<set>
#include<cstdio>
#include<iomanip>
#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
#define pb push_back
#define mk make_pair
#define ll long long
#define fi first
#define se second
#define PI 3.14159265
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#define eps 1e-7
#define pii pair<int,int>
#define pll pair<ll,ll>
typedef unsigned long long ull;
const int mod=998244353;
const ll inf=0x3f3f3f3f3f3f3f;
const int maxn=1e6+5;
using namespace std;
int n,m;
vector<int>g[maxn],G[maxn];
int dnf[maxn],low[maxn],ind,scc[maxn],scc_cnt,s[maxn],head,d[maxn];
void tarjan(int v,int f)
{
    dnf[v]=low[v]=++ind;
    s[++head]=v;
    for(int to :g[v])
    {
        if(f==to)continue;
        if(!dnf[to])
        {
            tarjan(to,v);
            low[v]=min(low[v],low[to]);
        }
        else low[v]=min(low[v],dnf[to]);
    }
    if(low[v]==dnf[v])
    {
        scc[v]=++scc_cnt;
        while(s[head]!=v)scc[s[head--]]=scc_cnt;
        head--;
    }
}
void dfs(int v,int f,int dep)
{
    d[v]=dep;
    for(int to:G[v])
    {
        if(to!=f)dfs(to,v,dep+1);
    }
}
int main()
{
    /* ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);*/
    //cin>>n>>m;
    scanf("%d %d",&n,&m);
    //cout<<d[(int)1e9]<<endl;
    for(int i=0;i<m;i++)
    {
        int x,y;
        //cin>>x>>y;
        scanf("%d %d",&x,&y);
        g[x].pb(y);g[y].pb(x);
    }
    tarjan(1,0); //cout<<"SSS"<<endl;
    for(int i=1;i<=n;i++)
    {
        for(int to:g[i])
        {
            if(scc[i]!=scc[to])
            {
                G[scc[i]].pb(scc[to]);
            }
        }
    }
    dfs(1,0,0);
    int tmp=0,s;
    for(int i=1;i<=scc_cnt;i++)
    {
        if(d[i]>tmp)s=i,tmp=d[i];
    }
    for(int i=0;i<=scc_cnt;i++)d[i]=0;
    dfs(s,0,0);
    int ans=0;
    for(int i=1;i<=scc_cnt;i++)
    {
        ans=max(d[i],ans);
    }
    printf("%d\n",ans);
    return 0;
}

有个相似的题

题目链接:E. Anton and Tree

题意:就是一次操作选中一个点并把所有到这个点最短路经过是颜色相同的点颜色反转,问至少几次操作把所有的颜色变为相同的点

题解:把相同颜色的相邻的点缩成一个点然后求树的直径,答案时(直径+1)/2

#include<bits/stdc++.h>
#include<set>
#include<cstdio>
#include<iomanip>
#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
#define pb push_back
#define mk make_pair
#define ll long long
#define fi first
#define se second
#define PI 3.14159265
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#define eps 1e-7
#define pii pair<int,int>
#define pll pair<ll,ll>
typedef unsigned long long ull;
const int mod=998244353;
const ll inf=0x3f3f3f3f3f3f3f;
const int maxn=1e6+5;
using namespace std;
int n,m;
vector<int>g[maxn],G[maxn];
int c[maxn];
int sc[maxn],cnt;
int dis[maxn];
bool vis[maxn];
void dfs(int v,int f,int ind,int cm)
{
    sc[v]=ind;
    vis[v]=true;
    for(int to:g[v])
    {
        if(to==f)continue;
        if(!vis[to]&&c[to]==cm)dfs(to,v,ind,cm);
    }
}
void dfs1(int v,int f,int d)
{
    dis[v]=d;
    for(int to:G[v])
    {
        if(to==f)continue;
            dfs1(to,v,d+1);
    }
}
int main()
{
     ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    cin>>n;
    for(int i=1;i<=n;i++)cin>>c[i];
    for(int i=0;i<n-1;i++)
    {
        int x,y;
        cin>>x>>y;
        g[x].pb(y);g[y].pb(x);
    }
    for(int i=1;i<=n;i++)
    {
        if(!vis[i]) dfs(i,0,++cnt,c[i]);
    }
    for(int i=1;i<=n;i++)
    {
        for(int to:g[i])
        {
            if(sc[to]!=sc[i])
            {
                G[sc[i]].pb(sc[to]);
            }
        }
    }
    dfs1(1,0,0);int tmp=0,s;
    for(int i=1;i<=cnt;i++)
    {
        if(dis[i]>tmp)s=i,tmp=dis[i];
    }
    memset(dis,0,sizeof(dis));
    dfs1(s,0,0);
    int ans=0;
    for(int i=1;i<=cnt;i++)
    {
        ans=max(ans,dis[i]);
    }
    cout<<(ans+1)/2<<endl;
    return 0;
}

  

原文地址:https://www.cnblogs.com/lhclqslove/p/9295804.html

时间: 2024-11-09 01:38:43

Educational Codeforces Round 46 (Rated for Div. 2)E. We Need More Bosses的相关文章

Educational Codeforces Round 46 (Rated for Div. 2) D. Yet Another Problem On a Subsequence

这个题是dp, dp[i]代表以i开始的符合要求的字符串数 j是我们列举出的i之后一个字符串的开始地址,这里的C是组合数 dp[i] += C(j - i - 1, A[i]] )* dp[j]; #include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include &

Educational Codeforces Round 46 (Rated for Div. 2) D

dp[i]表示一定包含第I个点的好的子序列个数,那么最终答案就是求dp[0] + dp[1] + .... + dp[n-1] 最终的子序列被分成了很多块,因此很明显我们枚举第一块,第一块和剩下的再去组合,然后我们为了保证没有重复,我们需要保证第一块不同,然而第一块的大小是固定的,因此我们可以选择枚举第一块最后一个数,这样第一块就肯定不会相同了,也可以计算 const ll P = 998244353; ll dp[maxn]; int N = 1000; ll comb[maxn][maxn]

Educational Codeforces Round 36 (Rated for Div. 2)

Educational Codeforces Round 36 (Rated for Div. 2) F. Imbalance Value of a Tree You are given a tree T consisting of n vertices. A number is written on each vertex; the number written on vertex i is ai. Let's denote the function I(x,?y) as the differ

Educational Codeforces Round 69 (Rated for Div. 2) B - Pillars

Educational Codeforces Round 69 (Rated for Div. 2) B - Pillars There are n pillars aligned in a row and numbered from 1 to n. Initially each pillar contains exactly one disk. The i-th pillar contains a disk having radius ai. You can move these disks

Educational Codeforces Round 71 (Rated for Div. 2) A - There Are Two Types Of Burgers

原文链接:https://www.cnblogs.com/xwl3109377858/p/11404050.html Educational Codeforces Round 71 (Rated for Div. 2) A - There Are Two Types Of Burgers There are two types of burgers in your restaurant — hamburgers and chicken burgers! To assemble a hamburg

Educational Codeforces Round 71 (Rated for Div. 2) D - Number Of Permutations

原文链接:https://www.cnblogs.com/xwl3109377858/p/11405773.html Educational Codeforces Round 71 (Rated for Div. 2) D - Number Of Permutations You are given a sequence of n pairs of integers: (a1,b1),(a2,b2),…,(an,bn). This sequence is called bad if it is

Educational Codeforces Round 72 (Rated for Div. 2)-D. Coloring Edges-拓扑排序

Educational Codeforces Round 72 (Rated for Div. 2)-D. Coloring Edges-拓扑排序 [Problem Description] ? 给你一个有向图,给用最少的颜色给每条边染色,要保证不存在一个环中的所有边都是同一个颜色. [Solution] ? 用拓扑排序判断图中是否存在环,若图中不存在环,则所有边都是同一种颜色.否则,同一个环中,只要用两种颜色就可以满足题目条件,所以总的颜色数就是两种,对于一个环,一定会存在两种边:1.节点号小

Educational Codeforces Round 36 (Rated for Div. 2) 题解

Educational Codeforces Round 36 (Rated for Div. 2) 题目的质量很不错(不看题解做不出来,笑 Codeforces 920C 题意 给定一个\(1\)到\(n\)组成的数组,只可以交换某些相邻的位置,问是否可以将数组调整为升序的 解题思路 首先如果每个数都能通过交换到它应该到的位置,那么就可以调整为升序的. 但实际上交换是对称的,如果应该在的位置在当前位置前方的数都交换完成,那么整体就是排好序的,因为不可能所有不在相应位置的数都在相应位置的后方.

Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes

Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes 题目连接: http://codeforces.com/contest/985/problem/E Description Mishka received a gift of multicolored pencils for his birthday! Unfortunately he lives in a monochrome w