CF #541 D Gourmet choice

Posted ckxkexing

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CF #541 D Gourmet choice相关的知识,希望对你有一定的参考价值。

link:https://codeforces.com/contest/1131

 

题意:

给定一些大小比较,输出排名。

思路:

这道题我用的是拓扑排序,又因为有等于号的存在,我用了并查集。

结束后这道题惨遭fst,因为我拓扑排序本应只放入一个集合的代表点,但是我放入了多次。

技术图片
#include <algorithm>
#include  <iterator>
#include  <iostream>
#include   <cstring>
#include   <cstdlib>
#include   <iomanip>
#include    <bitset>
#include    <cctype>
#include    <cstdio>
#include    <string>
#include    <vector>
#include     <stack>
#include     <cmath>
#include     <queue>
#include      <list>
#include       <map>
#include       <set>
#include   <cassert>

/*

⊂_ヽ
  \\ Λ_Λ  来了老弟
   \(‘?‘)
    > ⌒ヽ
   /   へ\
   /  / \\
   ? ノ   ヽ_つ
  / /
  / /|
 ( (ヽ
 | |、\
 | 丿 \ ⌒)
 | |  ) /
‘ノ )  L?

*/

using namespace std;
#define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "
";
#define pb push_back
#define pq priority_queue



typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
//typedef __int128 bll;
typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii;
typedef pair<int,pii> p3;

//priority_queue<int> q;//这是一个大根堆q
//priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
#define fi first
#define se second
//#define endl ‘
‘

#define boost ios::sync_with_stdio(false);cin.tie(0)
#define rep(a, b, c) for(int a = (b); a <= (c); ++ a)
#define max3(a,b,c) max(max(a,b), c);
#define min3(a,b,c) min(min(a,b), c);


const ll oo = 1ll<<17;
const ll mos = 0x7FFFFFFF;  //2147483647
const ll nmos = 0x80000000;  //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3f; //18
const int mod = 1e8;
const double esp = 1e-8;
const double PI=acos(-1.0);
const double PHI=0.61803399;    //黄金分割点
const double tPHI=0.38196601;


template<typename T>
inline T read(T&x){
    x=0;int f=0;char ch=getchar();
    while (ch<0||ch>9) f|=(ch==-),ch=getchar();
    while (ch>=0&&ch<=9) x=x*10+ch-0,ch=getchar();
    return x=f?-x:x;
}

inline void cmax(int &x,int y){if(x<y)x=y;}
inline void cmax(ll &x,ll y){if(x<y)x=y;}
inline void cmin(int &x,int y){if(x>y)x=y;}
inline void cmin(ll &x,ll y){if(x>y)x=y;}

/*-----------------------showtime----------------------*/

            const int maxn = 1e3+9;
            char mp[maxn][maxn];
            int fa[maxn*2];
            int find(int x){
                if(fa[x] == x) return x;
                return fa[x] = find(fa[x]);
            }
            void uni(int x,int y){
                int fx = find(x);
                int fy = find(y);
                if(fx == fy) return ;
                fa[fx] = fy;
            }
            int ans[maxn*2],tmp[maxn*2];
            int du[maxn*2],vis[maxn*2];
            vector<int>G[maxn*2];
int main(){
            int n,m;
            scanf("%d%d", &n, &m);
            rep(i, 1, n) scanf("%s", mp[i] + 1);
            rep(i, 1, n+m) fa[i] = i;

            rep(i, 1, n) {
                rep(j, 1, m) {
                    if(mp[i][j] == =) uni(i, j+n);
                }
            }

            rep(i, 1, n) {
                rep(j, 1, m) {
                    if(mp[i][j] == =) continue;
                    int u = find(i), v = find(j+n);
                    if(mp[i][j] == >){
                        du[u]++;
                        G[v].pb(u);
                    }
                    else {
                        du[v]++;
                        G[u].pb(v);
                    }
                }
            }
            queue<pii>que;
            for(int i=1; i<=n+m; i++){
                if(du[find(i)] == 0 && vis[find(i)] == 0)
                {
                    que.push(pii(find(i),1)),vis[find(i)] = 1,tmp[find(i)] = 1;
                }
            }

            while(!que.empty()){
                int u = que.front().fi;int s = que.front().se; que.pop();

                for(int i=0; i<G[u].size(); i++){
                    int v = G[u][i];
                    du[v] --;
                    if(du[v] ==0 && vis[v] == 0) {
                        tmp[v] = s+1;
                        que.push(pii(v, s+1));
                        vis[v] = 1;
                    }
                }
            }

            int flag = 1;
            for(int i=1; i<=n+m; i++) if(vis[find(i)] == 0) flag = 0;
            if(flag == 0) puts("No");
            else {
                puts("Yes");
                for(int i=1; i<=n; i++) printf("%d ", tmp[find(i)]);
                puts("");
                for(int i=1; i<=m; i++) printf("%d ", tmp[find(i+n)]);
                puts("");
            }
            return 0;
}
View Code

 

以上是关于CF #541 D Gourmet choice的主要内容,如果未能解决你的问题,请参考以下文章

D. Gourmet choice并查集,拓扑结构

Gourmet choice DP+拓扑

CF #541div2 E

cf-Round541-Div2-F(并查集+静态链表)

万得国泰安Choice数据库查找代查

Codeforces Round #541 (Div. 2) (A~F)