zoukankan      html  css  js  c++  java
  • 银河英雄传说

    think twice,code once

    这道题一开始没想清楚,g[i]表示的是i前面的点,不包括i,

    对回溯理解不清,回溯就是递归回来到这里了,再处理一些事......

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cmath>
     4 using namespace std;
     5 const int maxn=30007;
     6 int fa[maxn],sz[maxn],g[maxn];
     7 int find(int x){
     8     if(fa[x]==x) return x;
     9     else{
    10         int tt=find(fa[x]);
    11         g[x]+=g[fa[x]];
    12         return fa[x]=tt;
    13     }
    14 }
    15 void merge(int x,int y){
    16     int fx=find(x);int fy=find(y);
    17     if(fx==fy) return;
    18     g[fx]+=sz[fy];fa[fx]=fy;sz[fy]+=sz[fx];sz[fx]=0;
    19 }
    20 int main(){
    21     for(int i=1;i<=maxn;i++){g[i]=0;sz[i]=1;fa[i]=i;}
    22     int t;cin>>t;
    23     while(t--){
    24         char a;cin>>a;
    25         int x,y;cin>>x>>y;
    26         if(a=='M'){
    27             merge(x,y);
    28         }
    29         if(a=='C'){
    30             int fx=find(x);int fy=find(y);
    31             if(fx!=fy) cout<<-1<<endl;
    32             else cout<<abs(g[x]-g[y])-1<<endl;
    33         }
    34     }
    35     return 0;
    36 }
  • 相关阅读:
    dedecms织梦建站总结
    ubuntu安装
    git命令
    关于测试日报
    Appium环境搭建(Windows版)
    jmeter,CSV数据加载、数据库连接、正则
    jmeter,参数、Bean Shell断言
    jmeter安装和组件说明
    造数据
    WEB UI自动化
  • 原文地址:https://www.cnblogs.com/lcan/p/9575445.html
Copyright © 2011-2022 走看看