ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Codeforces Round #692 (Div. 2)C. Peaceful Rooks(并查集求环数)

2020-12-21 22:30:34  阅读:249  来源: 互联网

标签:692 typedef Rooks const 并查 int long include Find


题目传送
题意:
给你一个n*n的矩阵,m个车,中几个车可以在每次可以在x轴上或者y轴走任意的长度。但是得保证一行或者一列中只能有一个车。现在问最少多少次把所有的车都移到主对角线上。

思路:

假如一个点(x,y)能直接移动到(x,x)或者(y,y)上的话,那么就直接移动,那么就只需要一次,但是如果不能移动的话(也就是说有其他棋子在那一行或者那一列中)那么这个点必须让一步,也就是多退一步,只要一个点让一步,那么其他点都可以一次性到达一个对角线上的点,然后退的这个点,又只需要只用一步就可以到达一个对角线上。退一步的前提条件是形成一个环,所以我们只用求有多少个环就可以了

AC代码

#include<bits/stdc++.h>
//#include<iostream>
//#include<stack>
//#include<algorithm>
//#include<queue>
//#include<deque>
//#include<vector>
//#include<map>
//#include<set>
//#include<cmath>
//#include<cstdio>
//#include<string>
//#include<cstring>
inline long long read(){char c = getchar();long long x = 0,s = 1;
while(c < '0' || c > '9') {if(c == '-') s = -1;c = getchar();}
while(c >= '0' && c <= '9') {x = x*10 + c -'0';c = getchar();}
return x*s;}
using namespace std;
#define NewNode (TreeNode *)malloc(sizeof(TreeNode))
#define Mem(a,b) memset(a,b,sizeof(a))
#define lowbit(x) (x)&(-x)
//#define int long long
const int N = 2e5 + 10;
const long long INFINF = 0x7f7f7f7f7f7f7f;
const int INF = 0x3f3f3f3f;
const double EPS = 1e-5;
const int mod = 1e9+7;
const double II = acos(-1);
const double PP = (II*1.0)/(180.00);
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> piil;
int Find[N];
int find(int x)
{
    if(x == Find[x]) return x;
    return x = Find[x] = find(Find[x]);
}
signed main()
{
    std::ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    int t;
    cin >> t;
    while(t--)
    {
        int n,m,num = 0,cyc = 0;
        cin >> n >> m;
        for(int i = 1;i <= n;i++) Find[i] = i;
        for(int i = 1;i <= m;i++)
        {
            int x,y;
            cin >> x >> y;
            if(x == y) num++;
            else
            {
                ll numx = find(x),numy = find(y);
                if(numx == numy) cyc++;
                else Find[x] = y;
            }
        }
        cout << m - num + cyc << endl;
    }
    return 0;
}

标签:692,typedef,Rooks,const,并查,int,long,include,Find
来源: https://blog.csdn.net/moasad/article/details/111501126

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有