signed

QiShunwang

“诚信为本、客户至上”

PTA——会场安排问题

2021/5/15 0:02:32   来源:

在这里插入图片描述
在这里插入图片描述

/*
 * @Description: 
 * @version: 
 * @Author: 
 * @Date: 2021-05-14 21:12:35
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2021-05-14 21:25:19
 */
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
bool cmp(pair<int, int> &a, pair<int, int> &b)
{
    return a.first < b.first;
}
int OK(int i, const vector<int> &color, const vector<vector<bool>> &arc, const int n)
{
    // 遍历与i连通的所有点,若同色则返回0
    for (int j = 0; j < n; ++j)
        if (arc[i][j] && color[i] == color[j])
            return 0;
    return 1;
}
int main()
{
    int n;
    cin >> n;
    vector<pair<int, int>> activity;
    for (int i = 0; i < n; i++)
    {
        int start, end;
        cin >> start >> end;
        activity.push_back(make_pair(start, end));
    }
    sort(activity.begin(), activity.end(), cmp);
    vector<vector<bool>> mat;
    vector<bool> temp(n, false);
    for (int i = 0; i < n; i++)
        mat.push_back(temp);
    for (int i = 0; i < n; i++)
    {
        for (int j = i + 1; j < n; j++)
        {
            if (activity[i].second > activity[j].first)
            {
                mat[i][j] = true;
                mat[j][i] = true;
            }
        }
    }
    vector<int> color(n, 0);
    int k = 0;
    // 表示图中还有尚未涂色的点
    int flag = 1;
    while (flag)
    {
        // 取下一种颜色,遍历所有结点,对能涂这种颜色且不发生冲突的结点全部涂上这种颜色
        k++;
        // 将flag先置为0,若后续有未涂色成功的点会改为1
        flag = 0;
        for (int i = 0; i < n; ++i)
        {
            if (color[i] == 0)
            {
                color[i] = k;
                // 若发生冲突,则取消这个结点的涂色,将标志置为1,说明还有未涂色点
                if (!OK(i, color, mat, n))
                {
                    color[i] = 0;
                    flag = 1;
                }
            }
        }
    }
    cout << k << endl;
    return 0;
}