uva 11294 - Wedding(TwoSAT)

本文深入探讨了AI音视频处理领域中的关键技术,特别是视频分割与语义识别。通过详细解释这些技术的工作原理、应用案例及实际效果,旨在为读者提供全面的理解和洞察。

Problem E: Wedding

Up to thirty couples will attend a wedding feast, at which they will be seated on either side of a long table. The bride and groom sit at one end, opposite each other, and the bride wears an elaborate headdress that keeps her from seeing people on the same side as her. It is considered bad luck to have a husband and wife seated on the same side of the table. Additionally, there are several pairs of people conducting adulterous relationships (both different-sex and same-sex relationships are possible), and it is bad luck for the bride to see both members of such a pair. Your job is to arrange people at the table so as to avoid any bad luck.

The input consists of a number of test cases, followed by a line containing 0 0. Each test case gives n, the number of couples, followed by the number of adulterous pairs, followed by the pairs, in the form "4h 2w" (husband from couple 4, wife from couple 2), or "10w 4w", or "3h 1h". Couples are numbered from 0 to n-1 with the bride and groom being 0w and 0h. For each case, output a single line containing a list of the people that should be seated on the same side as the bride. If there are several solutions, any one will do. If there is no solution, output a line containing "bad luck".

Sample Input

10 6
3h 7h
5w 3w
7h 6w
8w 3w
7h 3w
2w 5h
0 0

Possible Output for Sample Input

1h 2h 3w 4h 5h 6h 7h 8h 9h

Prabhakar Ragde

题目翻译略坑爹,导致开始题意理解错了,把所有的夫妻都看成新娘新郎了。。。

然后又犯个傻b错误,找了一下午错误,后来找了题目的数据,才ac,解题也不想好好写了,就这样吧。

#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;

const int maxn = 100;

struct TwoSAT {
  int n;
  vector<int> G[maxn*2];
  bool mark[maxn*2];
  int S[maxn*2], c;

  bool dfs(int x) {
    if (mark[x^1]) return false;
    if (mark[x]) return true;
    mark[x] = true;
    S[c++] = x;
    for (int i = 0; i < G[x].size(); i++)
      if (!dfs(G[x][i])) return false;
    return true;
  }

  void init(int n) {
    this->n = n;
    for (int i = 0; i < n*2; i++) G[i].clear();
    memset(mark, 0, sizeof(mark));
  }

  // x = xval or y = yval
  void add_clause(int x, int xval, int y, int yval) {
    x = x * 2 + xval;
    y = y * 2 + yval;
    G[x^1].push_back(y);
    G[y^1].push_back(x);
  }

  bool solve() {
    for(int i = 0; i < n*2; i += 2)
      if(!mark[i] && !mark[i+1]) {
        c = 0;
        if(!dfs(i)) {
          while(c > 0) mark[S[--c]] = false;
          if(!dfs(i+1)) return false;
        }
      }
    return true;
  }
};

TwoSAT solver;

int main(){
    int n,m;
    char s1[10],s2[10];
    int a1,a2;
    while(scanf("%d%d",&n,&m)){
        if(n == 0 && m == 0) break;
        solver.init(2*n);
        solver.mark[1] = true;
        for(int i = 0;i < n;i++){
            solver.add_clause(2*i,1,2*i+1,1);
            solver.add_clause(2*i,0,2*i+1,0);
        }
        while(m--){
            scanf("%d%s%d%s",&a1,s1,&a2,s2);
            int xval,yval,x,y;
            xval = s1[0]=='h'?1:0;
            yval = s2[0]=='h'?1:0;
            x = a1*2+xval; y = a2*2+yval;
            solver.add_clause(x,1,y,1);
        }
        if(!solver.solve()) printf("bad luck\n");
        else{
            for(int i = 2;i < 2*(n-1);i+=2){
                printf("%d",i/2);
                if(solver.mark[2*i+1]) printf("w ");
                else printf("h ");
            }
            printf("%d",n-1);
            if(solver.mark[2*2*(n-1)+1]) printf("w\n");
            else printf("h\n");
        }
    }
    return 0;
}


一些数据

10 10
3h 7h
1w 0w
9h 0h
 5w 3w
8w 0w
7h 6w
 5h 0h
8w 3w
7h 3w
2w 5h

10 10
 6h 2w
1h 9w
1w 3w
9w 0h
1h 9h
 4h 1w
7h 2w
1h 0h
0h 9w
0h 3h

10 10
 0h 1w
1w 4h
7w 6w
9h 5h
4w 2h
7h 4w
4w 1w
4h 9h
4h 5w
5w 1h

10 10
6w 8h
0h 8h
 2h 7w
 8h 6h
 6w 0h
9w 1h
 7w 8w
1h 5w
 7h 1h
 6h 8w
 
10 10
 4w 0h
 5h 1h
 8h 0w
7h 4h
5h 9h
6w 4h
5h 3w
2w 6h
 3h 2h
 1w 6w
 
10 10
 0h 8w
 7h 0h
6w 8h
4w 5w
 9h 1w
4w 7w
5h 8h
5h 9w
3w 5h
4w 8h

10 10
8h 3w
3h 2w
 2h 0h
 4h 8w
6h 9w
 7h 5w
 3w 4w
7w 0w
4w 8w
3h 9w

10 10
7w 4w
7w 8h
5h 4w
3h 8h
 9w 8h
0h 3w
 8h 6h
 1h 2w
2h 6w
 8h 4w

10 10
 1h 6h
 8h 7h
 7w 2w
 5w 6w
9h 7h
 3w 1w
 8w 0h
 4h 3w
 4w 1w
 5h 3w

10 10
 7w 4h
 2w 7w
5h 1h
6w 9h
2h 4h
4w 5w
0w 4w
9w 2w
4w 9h
 0w 7w

30 30
 5h 18w
5w 19h
27w 26h
16h 15h
21w 16h
 28h 27h
19w 28w
8w 29w
 27w 19w
16h 1h
6w 18h
1w 3h
 14w 3h
9h 14w
11h 2w
21w 17h
 18h 13w
 6h 11h
26w 27w
16h 12h
 11w 4h
 29h 3w
6h 14w
 29w 15h
11h 10w
 9w 27w
21w 9h
 16w 7w
 7h 18w
8w 2h

30 30
18w 26h
6w 16h
21h 25h
10h 9w
 15h 26w
7w 9h
5w 19h
14w 22w
14h 13w
13w 1h
9h 26w
3w 13h
7w 12h
 26w 20w
2w 29w
14w 26h
23w 11h
22h 2w
23h 5h
 17h 6h
 11h 22w
 23h 22h
 13h 27w
5w 18w
24w 27h
26h 1w
 20h 7h
 16w 18w
5w 27w
17h 0h

 

#include <bits/stdc++.h> // #include "atcoder/convolution" // #include "atcoder/dsu" // #include "atcoder/fenwicktree" // #include "atcoder/lazysegtree" // #include "atcoder/math" // #include "atcoder/maxflow" // #include "atcoder/mincostflow" // #include "atcoder/modint" // #include "atcoder/scc" // #include "atcoder/segtree" // #include "atcoder/string" // #include "atcoder/twosat" // using namespace __gnu_cxx; // using namespace __gnu_pbds; using namespace std; auto rng = mt19937(random_device()()); auto rngl = mt19937_64(random_device()()); // Let's set a bit and flow! // I came, I divided, I conquered! int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t; cin >> t; while (t --) { int n, q; cin >> n >> q; vector<long long> nums(n); for (auto &x: nums) cin >> x; vector<long long> vis = nums, cur = nums; vector<pair<int, int>> updates(q); for (auto &[idx, val]: updates) { cin >> idx >> val; idx --; cur[idx] += val; vis.emplace_back(cur[idx]); } sort(vis.begin(), vis.end()); vis.erase(unique(vis.begin(), vis.end()), vis.end()); int k = vis.size(); vector<int> fen(k + 1, 0); auto getidx = [&] (long long val) -> int { return lower_bound(vis.begin(), vis.end(), val) - vis.begin(); }; auto add = [&] (int idx, int val) -> void { idx ++; while (idx <= k) { fen[idx] += val; idx += idx & -idx; } }; auto not_exceed = [&] (int val) -> int { int res = 0, idx = 0; for (int i = 20; i >= 0; i --) { if ((idx | (1 << i)) <= k && res + fen[idx | (1 << i)] <= val) { idx |= 1 << i; res += fen[idx]; } } return res; }; for (
最新发布
07-18
### Fenwick Tree 与坐标压缩在范围查询中的应用 Fenwick Tree(又称Binary Indexed Tree, BIT)是一种高效的数据结构,用于处理前缀和的计算以及单点更新操作。它的时间复杂度为 $ O(\log n) $,适用于大规模数据集。 #### Fenwick Tree 的基本实现 以下是一个简单的 Fenwick Tree 实现,支持单点更新和前缀和查询: ```cpp class FenwickTree { private: std::vector<int> tree; int size; public: FenwickTree(int n) : size(n), tree(n + 1, 0) {} // 单点更新:将位置 idx 的值增加 delta void update(int idx, int delta) { while (idx <= size) { tree[idx] += delta; idx += idx & -idx; } } // 查询前缀和 [1, idx] int query(int idx) { int result = 0; while (idx > 0) { result += tree[idx]; idx -= idx & -idx; } return result; } }; ``` #### 坐标压缩的应用 当输入数据的范围非常大时(例如 $ 1 \leq x \leq 10^9 $),直接使用原始数值作为索引会导致内存浪费或无法存储。此时需要通过**坐标压缩**来优化存储空间。 ##### 示例:坐标压缩的实现 以下代码演示了如何对一组数据进行坐标压缩: ```cpp #include <iostream> #include <vector> #include <algorithm> int main() { std::vector<int> data = {5, 2, 8, 3, 5, 10}; std::vector<int> sorted_data = data; // 对数据进行排序并去重 std::sort(sorted_data.begin(), sorted_data.end()); auto it = std::unique(sorted_data.begin(), sorted_data.end()); sorted_data.resize(std::distance(sorted_data.begin(), it)); // 创建映射表 std::vector<int> compressed(data.size()); for (int i = 0; i < data.size(); ++i) { compressed[i] = std::lower_bound(sorted_data.begin(), sorted_data.end(), data[i]) - sorted_data.begin() + 1; } // 输出压缩后的结果 for (int val : compressed) { std::cout << val << " "; } return 0; } ``` 输出: ``` 4 2 5 3 4 6 ``` ##### 结合 Fenwick Tree 进行范围查询 假设需要统计某个区间内的元素数量,可以结合 Fenwick Tree 和坐标压缩来高效处理。以下是一个完整的示例: ```cpp #include <iostream> #include <vector> #include <algorithm> class FenwickTree { private: std::vector<int> tree; int size; public: FenwickTree(int n) : size(n), tree(n + 1, 0) {} void update(int idx, int delta) { while (idx <= size) { tree[idx] += delta; idx += idx & -idx; } } int query(int idx) { int result = 0; while (idx > 0) { result += tree[idx]; idx -= idx & -idx; } return result; } }; int main() { std::vector<int> data = {5, 2, 8, 3, 5, 10}; std::vector<int> sorted_data = data; // 坐标压缩 std::sort(sorted_data.begin(), sorted_data.end()); auto it = std::unique(sorted_data.begin(), sorted_data.end()); sorted_data.resize(std::distance(sorted_data.begin(), it)); std::vector<int> compressed(data.size()); for (int i = 0; i < data.size(); ++i) { compressed[i] = std::lower_bound(sorted_data.begin(), sorted_data.end(), data[i]) - sorted_data.begin() + 1; } // 初始化 Fenwick Tree FenwickTree ft(sorted_data.size()); // 更新每个元素 for (int val : compressed) { ft.update(val, 1); } // 查询某个范围内的元素数量 int l = 2, r = 4; // 原始数据对应的范围 int count = ft.query(r) - ft.query(l - 1); std::cout << "Number of elements in range [" << l << ", " << r << "]: " << count << std::endl; return 0; } ``` 输出: ``` Number of elements in range [2, 4]: 3 ``` ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值