【学习笔记】CF627F Island Puzzle

好啊,树上贪心题。

首先可以用类似于拓扑排序的过程将无用的节点全部删掉。

具体的,如果两个叶子节点对应的值恰好相同,那么同时将叶子节点删去;如果其中一个叶子节点对应的是 0 0 0,并且与父节点交换后相同,因为操作是可逆的,那么花费 1 1 1的代价将 0 0 0往中间挪,同时删去叶子节点;如果两个叶子节点对应的都是 0 0 0,那么有挪和不挪两种情况,如果不挪可以把整颗树删完那么就结束了,否则可以认为此时两个 0 0 0同时往中间挪。

观察到此时连一条边只能改变一个环上的点,因此如果有解那么一定只有两个叶子节点,也就是形成了一条路径。那么我们将路径连起来就形成了一个环。观察到 0 0 0只会往一个方向移动,一个数向左/右挪动的距离就是被交换的次数,因此贪心即可。

然后就做完了。

复杂度 O ( n ) O(n) O(n)

#include<bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define pb push_back
#define inf 0x3f3f3f3f3f3f3f3f
#define db double
#define cpx complex<db>
using namespace std;
const int N=2e5+5;
int n,a[N],b[N],totleaf,home[N],deg[N];
int aa[N],bb[N],cnt,rk[N];
ll res;
vector<int>G[N];
queue<int>Q;
vector<int>vec;
void dfs(int u){
    home[u]=1;
    aa[cnt]=a[u],bb[cnt]=b[u],cnt++;
    for(auto v:G[u]){
        if(!home[v])dfs(v);
    }
}
ll solve(){
    memset(rk,-1,sizeof rk);
    int p=0;while(p<cnt&&bb[p])p++;
    for(int i=0;i<cnt;i++)rk[aa[i]]=i;
    ll Min=inf,Max=-inf;
    for(int i=0;i<cnt;i++){
        if(bb[i]){
            ll D=(i-rk[bb[i]]+cnt)%cnt;
            Min=min(Min,D),Max=max(Max,D); 
        }
    }
    if(Max-Min>1){
        return inf;
    }
    if(Max==Min){
        return Max*(cnt-1);
    }
    else{
        int cnt1=0,cnt2=0;
        for(int i=(p+1)%cnt;i!=p;i=(i+1)%cnt){
            ll D=(i-rk[bb[i]]+cnt)%cnt;
            if(D==Max)cnt1++;
        }
        for(int i=(p+1)%cnt;i!=p;i=(i+1)%cnt){
            ll D=(i-rk[bb[i]]+cnt)%cnt;
            if(D==Max)cnt2++;
            else break;
        }
        if(cnt1!=cnt2)return inf;
        return Min*(cnt-1)+cnt1;
    }
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    cin>>n;for(int i=1;i<=n;i++)cin>>a[i];
    for(int i=1;i<=n;i++)cin>>b[i];
    for(int i=1;i<n;i++){
        int u,v;cin>>u>>v;
        G[u].pb(v),G[v].pb(u);
        deg[u]++,deg[v]++;
    }
    for(int i=1;i<=n;i++){
        if(deg[i]==1){
            totleaf++;
            Q.push(i);
        }
    }
    while(Q.size()){
        int x=Q.front();Q.pop();
        if(!a[x]&&!b[x])continue;
        if(a[x]==b[x]){
            home[x]=1,totleaf--;
            for(auto y:G[x]){
                if(!home[y]&&--deg[y]==1){
                    Q.push(y),totleaf++;
                }
            }
        }
        else if(a[x]==0){
            int fa=0;
            for(auto y:G[x]){
                if(!home[y]){
                    fa=y;
                }
            }
            if(a[fa]==b[x]){
                swap(a[x],a[fa]),res++,home[x]=1,totleaf--;
                if(--deg[fa]==1){
                    Q.push(fa),totleaf++;
                }
            }
        }
        else if(b[x]==0){
            int fa=0;
            for(auto y:G[x]){
                if(!home[y]){
                    fa=y;
                }
            }
            if(b[fa]==a[x]){
                swap(b[x],b[fa]),res++,home[x]=1,totleaf--;
                if(--deg[fa]==1){
                    Q.push(fa),totleaf++;
                }
            }
        }
    }
    if(totleaf==1){
        cout<<0<<" "<<res<<"\n";
    }
    else{
        for(int i=1;i<=n;i++){
            if(a[i]==0&&b[i]==0&&deg[i]==1)Q.push(i);
        }
        while(Q.size()){
            int x=Q.front();Q.pop();
            int fa=0;
            for(auto y:G[x]){
                if(!home[y]){
                    fa=y;
                }
            }
            if(a[fa]==b[fa]){
                swap(a[x],a[fa]),swap(b[x],b[fa]),home[x]=1,res+=2,totleaf--;
                if(--deg[fa]==1)Q.push(fa),totleaf++;
            }
        }
        if(totleaf>2){
            cout<<-1<<"\n";
        }
        else{
            for(int i=1;i<=n;i++){
                if(deg[i]==1&&!home[i]){
                    vec.pb(i);
                }
            }
            dfs(vec[0]);
            ll res1=solve();
            reverse(aa,aa+cnt),reverse(bb,bb+cnt);
            res1=min(res1,solve());
            if(res1==inf){
                cout<<-1<<"\n";
            }
            else{
                cout<<vec[0]<<" "<<vec[1]<<" "<<res+res1<<"\n";
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
单词搜索迷宫(Word Search Puzzle)问题是一个经典的算法问题,其输入是一个二维的字符数组和一组单词,目标是找出字符数组网格中的所有单词。这些单词可以是水平的、垂直的或者是任意的对角线方向,所以需要查找8个不同的方向。解决这个问题的一种常见方法是使用回溯算法,具体步骤如下: 1. 遍历二维字符数组,对于每个字符,以其为起点开始搜索,搜索的方向包括水平、垂直和对角线方向。 2. 对于每个搜索到的单词,将其记录下来。 3. 重复步骤1和2,直到遍历完整个二维字符数组。 下面是一个使用C#语言实现的单词搜索迷宫算法的示例代码: ```csharp class WordSearchPuzzle { private char[,] grid; private HashSet<string> words; public WordSearchPuzzle(char[,] grid, HashSet<string> words) { this.grid = grid; this.words = words; } public void Solve() { int rows = grid.GetLength(0); int cols = grid.GetLength(1); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { Search(i, j, new StringBuilder()); } } } private void Search(int row, int col, StringBuilder sb) { if (row < 0 || row >= grid.GetLength(0) || col < 0 || col >= grid.GetLength(1)) { return; } sb.Append(grid[row, col]); string word = sb.ToString(); if (words.Contains(word)) { Console.WriteLine("Found '{0}' at [{1}, {2}] to [{3}, {4}]", word, row, col, row - sb.Length + 1, col - sb.Length + 1); } if (word.Length < 3) { Search(row + 1, col, sb); Search(row - 1, col, sb); Search(row, col + 1, sb); Search(row, col - 1, sb); Search(row + 1, col + 1, sb); Search(row - 1, col - 1, sb); Search(row + 1, col - 1, sb); Search(row - 1, col + 1, sb); } sb.Remove(sb.Length - 1, 1); } } // 使用示例 char[,] grid = new char[,] { {'t', 'h', 'i', 's'}, {'w', 'a', 't', 's'}, {'o', 'a', 'h', 'g'}, {'f', 'g', 'd', 't'} }; HashSet<string> words = new HashSet<string>() { "this", "two", "fat", "that" }; WordSearchPuzzle puzzle = new WordSearchPuzzle(grid, words); puzzle.Solve(); ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值