Gym 102055B Balance of the Force

大意: n个骑士, 第i个骑士若加入光明阵营, 那么能力值Li, 加入黑暗阵营, 能力值Di. 给定m个限制(ui,vi), 表示ui,vi不能在同一阵营. 求一种划分方案, 使得能力值最大值减最小值最小.

 

对于一个连通块, 如果不是二分图, 那么无解. 否则的话这个连通块最大值最小值只有两种情况, 枚举最大值, 求出最小值的最大可能值更新答案即可.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <cstring>
#include <bitset>
#include <functional>
#include <random>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
#define DB(a) ({REP(__i,1,n) cout<<a[__i]<<',';hr;})
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;}
//head
 
 
 
const int N = 1e6+50;
int n,m,ok,vis[N],l[N],d[N],mi[N],ID[N],cur[N];
vector<int> g[N];
pii f[N],A,B;
 
void dfs(int x, int c) {
    vis[x] = c;
    if (c) {
        A.x = min(A.x,l[x]);
        A.y = max(A.y,l[x]);
        B.x = min(B.x,d[x]);
        B.y = max(B.y,d[x]);
    }
    else {
        A.x = min(A.x,d[x]);
        A.y = max(A.y,d[x]);
        B.x = min(B.x,l[x]);
        B.y = max(B.y,l[x]);
    }
    for (int y:g[x]) {
        if (vis[y]<0) dfs(y,c^1);
        else if (vis[y]==c) ok=0;
    }
}
 
void work() {
    scanf("%d%d",&n,&m);
    REP(i,1,n) vis[i]=-1,g[i].clear();
    REP(i,1,m) {
        int u, v;
        scanf("%d%d",&u,&v);
        g[u].pb(v),g[v].pb(u);
    }
    REP(i,1,n) scanf("%d%d",l+i,d+i);
    ok = 1;
    vector<pii> events;
    int cnt = 0;
    multiset<int> s;
    REP(i,1,n) if (vis[i]<0) {
        A = B = {1e9,0};
        dfs(i, 0);
        if (!ok) return puts("IMPOSSIBLE"),void();
        s.insert(cur[i]=-INF);
        ID[cnt]=i,mi[cnt]=A.x,events.pb(pii(A.y,cnt)),++cnt;
        ID[cnt]=i,mi[cnt]=B.x,events.pb(pii(B.y,cnt)),++cnt;
    }
    sort(events.begin(),events.end());
    int ans = 1e9;
    for (auto &p:events) {
        s.erase(s.find(cur[ID[p.y]]));
        cur[ID[p.y]] = max(cur[ID[p.y]], mi[p.y]);
        s.insert(cur[ID[p.y]]);
        ans = min(ans, p.x-*s.begin());
    }
    printf("%d\n", ans);
}
 
int main() {
    int t=rd();
    REP(i,1,t) {
        printf("Case %d: ",i);
        work();
    }
}

 

posted @   uid001  阅读(309)  评论(0编辑  收藏  举报
(评论功能已被禁用)
编辑推荐:
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
· dotnet 源代码生成器分析器入门
阅读排行:
· ThreeJs-16智慧城市项目(重磅以及未来发展ai)
· .NET 原生驾驭 AI 新基建实战系列(一):向量数据库的应用与畅想
· Browser-use 详细介绍&使用文档
· 软件产品开发中常见的10个问题及处理方法
· Vite CVE-2025-30208 安全漏洞
点击右上角即可分享
微信分享提示