2019 CCPC-江西省赛(填坑中)

Cotree

题意:

由N个点构成两棵树,问在两颗树之间连接一条边之后,各点之间距离和的最小值为多少

题解:

 进行两次DFS找到两颗树的重心,将两个重心连接起来,再进行一次DFS求出距离和即可

#include <iostream>
#include <map>
#include <vector>
#include <queue>
#include <string>
#include <set>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <list>
#include <deque>
#include <queue>
#include <stack>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <iomanip>
#pragma GCC optimize(2)
#define ll long long
#define ull unsigned long long
using namespace std;
template <class T>
void rd(T &x){
    x = 0 ;T flag = 1 ;char ch = getchar();
    while(!isdigit(ch)) { if(ch=='-') flag = -1 ; ch = getchar() ; }
    while(isdigit(ch)) { x = (x<<1) + (x<<3) + (ch^48) ; ch = getchar(); }
    x*= flag ;
}
const int INF=0x3f3f3f3f;
const int mxn = 1e5+7;
ll t,n,m,k,cnt,node_1,node_2,ans_1,ans_2,mn_son = 0 , mn_node = 0 , res , ans ;
int head[mxn<<2] , to[mxn<<2] , nx[mxn<<2] ,deg[mxn<<2] ;
int vis[mxn<<2] ;
void add(int u,int v){ to[cnt] = v , nx[cnt] = head[u] , head[u] = cnt++; }
void DFS_INIT(int x) /// 计算两颗子树的节点数量
{
    if(vis[x]) return ;
    res++; vis[x] = 1 ;
    for(int i=head[x];~i;i=nx[i]) if(!vis[to[i]]) DFS_INIT(to[i]); /// 
}
void DFS(int x,int par,int n)
{
    deg[x]=1; int mx_son = 0 ;
    for(int i=head[x];~i;i=nx[i]){
        int v = to[i] ;
        if(v==par) continue;
        DFS(v,x,n);
        deg[x] += deg[v] ;
        mx_son = max(mx_son , deg[v]);/// 子树的最大节点个数
    }
    mx_son = max(mx_son,n-deg[x]); /// 子树的最大节点个数
    if( mx_son < mn_son ){
        mn_son = mx_son , mn_node = x ;
    }
}
void DFS_END(int x,int par)
{
    deg[x] = 1 ;
    for(int i=head[x];~i;i=nx[i]){
        int v = to[i] ;
        if(v==par) continue;
        DFS_END(v,x) ;
        deg[x] += deg[v];
        ans+=(ll)(deg[v])*(ll)(n-deg[v]);
    }
}
int main()
{
    while(~scanf("%lld",&n)){
        cnt = 0 ;
        memset(deg,0,sizeof(deg));
        memset(head,-1,sizeof(head));
        for(int i=1,u,v;i<=n-2;i++){
            rd(u) , rd(v) ;
            add(u,v) , add(v,u) ;
        }
        memset(vis,0,sizeof(vis));
        int n1 = 0 , n2 = 0 ; res = 0 ;
        DFS_INIT(1) , node_1 = 1 , n1 = res , res = 0 ;
        for(int i=1;i<=n;i++){
            if(!vis[i]) {
                DFS_INIT(i) , n2 = res , node_2 = i , res = 0 ;
                break;
            }
        }
        mn_son = INF ; int e1 , e2 ;
        DFS(node_1,0,n1) , e1 = mn_node ,mn_son = INF ;
        DFS(node_2,0,n2) , e2 = mn_node ,mn_son = INF ;
        ///cout<<e1<<" -- "<<e2<<endl;
        add(e1,e2) , add(e2,e1);
        ans = 0 ; DFS_END(1,0);
        printf("%lld\n",ans);
    }
    return 0;
}
View Code

 

Wave

题意:

从给定的序列中找出一个最长子序列(可不连续),使得子序列的奇数位相同,偶数位相同,相邻位置不同

题解:

将每个数 X 的下标存到一个数组中,枚举交替的数字,二分查找位置

Traffic

题意:

给定数列 a,b,查找一个数 X ,使得 b 数组整体加 X ,且与a数组没有相同的数,X最小值为多少

题解:

枚举 X ,判断是否有交集即可

Rng

题意:

从【1,N】选择 r ,再从【1,r】选择 l ,重复选择两次,两次选择的区间【l,r】的交集的概率是多少

题解:

#include <iostream>
#include <map>
#include <vector>
#include <queue>
#include <string>
#include <set>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <list>
#include <deque>
#include <queue>
#include <stack>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <iomanip>
#pragma GCC optimize(2)
using namespace std;
const int inf=0x3f3f3f3f;
#define ll long long
#define ull unsigned long long
const int mod = 1e9+7;/// 998244353;
const int mxn = 1e5 +7;
int _ , n , m , t , k , ans , cnt , si , res ;
template <class T>
void rd(T &x){
    T flag = 1 ; x = 0 ; char ch = getchar() ;
    while(!isdigit(ch)) { if(ch=='-') flag = -1; ch = getchar(); }
    while(isdigit(ch)) { x = (x<<1) + (x<<3) + (ch^48); ch = getchar(); }
    x*=flag;
}
ll ksm(ll a,ll b,ll mod)
{
    ll ans = 1 ;
    while(b){
        if(b&1) ans = ans * a % mod ;
        a = a*a%mod ;
        b>>=1;
    } 
    return ans ;
}
void solve()
{
    while(cin>>n)
        cout<<((n+1)%mod*ksm(2*n%mod,mod-2,mod)%mod)<<endl;
}
int main()
{
    /// freopen("input.in","r",stdin) ; freopen("output.in","w",stdout) ;
    ios::sync_with_stdio(false); cin.tie(0) ; cout.tie(0);
    solve();
}
View Code

 

posted @ 2020-09-14 22:48  __MEET  阅读(188)  评论(0编辑  收藏  举报