数石子RQNOJ 并查集

巧妙的并查集。对于 i,j,k  可以表示  d[j] - d[i-1]= k;若i,j有公共祖先 则可以求出 分别到 祖先的距离,差值就是答案。

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<queue>
#include<stack>
#include<list>
#include<stdlib.h>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include <fstream>
using namespace std;

const int Maxn=5555;

int father[Maxn];
int dist[Maxn];
int getfather(int x)
{
    //cout<<x<<endl;system("pause");
    if(x==father[x]) return x;
    int t=getfather(father[x]);
    dist[x]+=dist[father[x]];
    return father[x]=t;
}

int main()
{
    int n,m,k;
    int a,b,c;
    scanf("%d%d%d",&n,&m,&k);
    for(int i=0;i<=n;i++)
        father[i]=i;
    memset(dist,0,sizeof(dist));
    for(int i=0;i<m;i++){
        scanf("%d%d%d",&a,&b,&c);
        int fa=getfather(a-1);int fb=getfather(b);
        if(fa==fb) continue;
        father[fa]=fb;
        dist[fa]=c+dist[b]-dist[a-1];
    }
    for(int i=0;i<k;i++){
        scanf("%d%d",&a,&b);
        int fa=getfather(a-1);int fb=getfather(b);
        if(fa!=fb) {
            printf("UNKNOWN\n");
        }
        else{
            printf("%d\n",dist[a-1]-dist[b]);
        }
    }
    return 0;
}

  

转载于:https://www.cnblogs.com/yigexigua/p/3845181.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
很抱歉,我似乎没有找到与彩旗飘飘Python相关的引用内容。但是,如果你想在Python中实现彩旗飘飘的效果,你可以使用Pygame库来创建动画。以下是一个简单的示例代码,可以帮助你开始: ```python import pygame import random # 初始化pygame pygame.init() # 设置屏幕尺寸 screen_width = 800 screen_height = 600 screen = pygame.display.set_mode((screen_width, screen_height)) pygame.display.set_caption("彩旗飘飘") # 加载彩旗图片 flag_image = pygame.image.load("flag.png") # 获取彩旗图片的宽度和高度 flag_width = flag_image.get_width() flag_height = flag_image.get_height() # 设置彩旗的起始位置 flag_x = screen_width // 2 - flag_width // 2 flag_y = screen_height // 2 - flag_height // 2 # 设置彩旗的速度 flag_speed_x = random.randint(-5, 5) flag_speed_y = random.randint(-5, 5) # 游戏主循环 running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # 移动彩旗 flag_x += flag_speed_x flag_y += flag_speed_y # 如果彩旗碰到边界,反转速度方向 if flag_x <= 0 or flag_x + flag_width >= screen_width: flag_speed_x = -flag_speed_x if flag_y <= 0 or flag_y + flag_height >= screen_height: flag_speed_y = -flag_speed_y # 清空屏幕 screen.fill((255, 255, 255)) # 绘制彩旗 screen.blit(flag_image, (flag_x, flag_y)) # 更新屏幕 pygame.display.flip() # 退出游戏 pygame.quit() ``` 请注意,你需要将代码中的"flag.png"替换为你自己的彩旗图片。此外,你还可以根据需要调整彩旗的起始位置、速度等参。希望这个示例代码能帮助到你!<span class="em">1</span><span class="em">2</span> #### 引用[.reference_title] - *1* [3d max制作彩旗飘飘](https://download.csdn.net/download/m0_71585230/88011500)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [[Rqnoj-371][AHOI1997]彩旗飘飘](https://blog.csdn.net/w745241408/article/details/7176600)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值