首页 > 其他分享 >D - Pair of Balls

D - Pair of Balls

时间:2023-02-08 22:34:35浏览次数:38  
标签:cylinder Balls color int front second Pair first

D - Pair of Balls

https://atcoder.jp/contests/abc216/tasks/abc216_d

 

思路

m个桶, n个颜色,

第一轮遍历所有桶的最上面球的颜色,

将相同颜色的球所在队列, 计入 color节点,

即是说,存在两个桶的最上面的两个球颜色相同, 例如 a11 和 ax1 都是颜色3

那么3颜色计入任务处理队列

对任务队列处理后, 1 和 x桶做pop顶部节点, 然后将1 和 x桶的新的顶部颜色 计入 color, 如果颜色达到两个, 计入任务处理队列,

继续处理队列,直至队列为空。

 

最后检测所有桶是否为空, 如果有一个桶不为空, 则输出No

否则输出Yes

 

 

 

 

 

Code

https://atcoder.jp/contests/abc216/submissions/38719333

int n, m;

int main()
{

    cin >> n >> m;

    vector<queue<int> > cylinders(m+1);
    
    for(int i=1; i<=m; i++){
        int ki;
        
        cin >> ki;
        
        for(int j=1; j<=ki; j++){
            int aij;
            cin >> aij;
            
            queue<int>& cylinder_i = cylinders[i];
            cylinder_i.push(aij);
        }
        
//        cout << "cylinders i size=" << cylinders[i].size() << endl;
    }

    vector< vector<int> > colors(n+1);
    queue<int> tasks;

    for(int i=1; i<=m; i++){
        queue<int>& cylinder_i = cylinders[i];
        
        if (cylinder_i.empty()){
            continue;
        }
        
        int color = cylinder_i.front();
        
        colors[color].push_back(i);
        
        if(colors[color].size() == 2){
            tasks.push(color);
//            cout << "got color =" << color << endl;
        }
    }

    while(!tasks.empty()){
        int color_hit = tasks.front();
//        cout << "color_hit =" << color_hit << endl;
        
        tasks.pop();
        
        vector<int>& two_cylinders = colors[color_hit];
        int first_cylinder_num = two_cylinders[0];
        int second_cylinder_num = two_cylinders[1];

        queue<int>& first_cylinder = cylinders[first_cylinder_num];
        queue<int>& second_cylinder = cylinders[second_cylinder_num];
        
        first_cylinder.pop();
        second_cylinder.pop();
        
        if (!first_cylinder.empty()){
            int first_cylinder_front_color = first_cylinder.front();
            colors[first_cylinder_front_color].push_back(first_cylinder_num);
            
            if (colors[first_cylinder_front_color].size() == 2){
                tasks.push(first_cylinder_front_color);
            }
        }
        
        if (!second_cylinder.empty()){
            int second_cylinder_front_color = second_cylinder.front();
            colors[second_cylinder_front_color].push_back(second_cylinder_num);
            
            if (colors[second_cylinder_front_color].size() == 2){
                tasks.push(second_cylinder_front_color);
            }
        }
    }
    
    for(int i=1; i<=m; i++){
//        cout << "cylinders i size=" << cylinders[i].size() << endl;
        if (!cylinders[i].empty()){
            cout << "No" << endl;
            return 0;
        }
    }

    cout << "Yes" << endl;

    return 0;
}

 

标签:cylinder,Balls,color,int,front,second,Pair,first
From: https://www.cnblogs.com/lightsong/p/17103575.html

相关文章