CodeForces-Learning Languages

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/*
这道题目考查并查集,特别注意每个人都不会语言的情况,此时结果为n。
*/
int father[105];
int lang[105][105];
int flag[105];
int set[105];

int find(int x){
    int r = x;
    while(father[r]!=r)
        r=father[r];
    //压缩
    int i = x,j;
    while(father[i]!=r){
        j=father[i];
        father[i]=r;
        i=j;
    }
    return r;
}

void merge(int x,int y){
    int fx = find(x);
    int fy = find(y);
    if (fx!=fy)
        father[fx]=fy;
}

int main(){
    freopen("0input.txt","r",stdin);
    int n,m,k,p,res,flag1=0;
    while(scanf("%d%d",&n,&m)!=EOF){
        //初始化
        flag1=0;
        memset(flag,0,sizeof(flag));
        memset(lang,0,sizeof(lang));
        memset(set,0,sizeof(set));
        for (int i = 1; i <= n; ++i)
            father[i]=i;
        //读入数据
        for (int i = 1; i <= n; ++i)
        {
            scanf("%d",&k);
            if (k!=0)
            {
                flag[i]=1;
                for (int j = 1; j <= k; ++j)
                {
                    scanf("%d",&p);
                    lang[p][i]=1;
                }
            }
        }

        //构造并查集
        for (int i = 1; i <=m; ++i)
        {
            int j,fa,fb;
            for (j = 1; j <= n; ++j)
            {
                if (lang[i][j]){

                    fa = find(j);
                    //printf("fa:%d\n",fa );
                    break;
                }
            }
            for (j=j+1; j <= n; ++j)
            {
                if (lang[i][j])
                {
                    fb=find(j);
                    //printf("fa:%dfb:%d\n",fa,fb);
                    merge(fa,fb);
                }
            }
        }
        for (int i = 1; i <= n; ++i)
        {
            //printf("flag:%d ",flag[i]);
            if (flag[i]){
                flag1=1;
                break;
            }
        }

        if (!flag1)
        {
            printf("%d\n",n);
        }else{

            //统计将员工分成几类
            int fc;
            for (int i = 1; i <=n; ++i)
            {
                fc = find(i);
                set[fc]++;
            }
            res =0;
            for (int i = 1; i <= n; ++i)
            {
                if (set[i]>=1)
                    res++;
            }
            printf("%d\n",res-1);
        }

    }
    return 0;
}
时间: 2024-12-16 07:45:12

CodeForces-Learning Languages的相关文章

Codeforces 278C Learning Languages(并查集) 求连通块

Codeforces 278C Learning Languages(并查集) 求连通块 为什么最后还要getfather 一遍 比如 x 是 y 的父亲 然后你 Union(x,z) 然后 z 变成了 x 父亲 然后 y 的祖先就是错的了 题解 求一个无向图中有几个连通块 sum 特判 一下 如果 每一个人的语言都为 0 则答案为 sum 其他 答案则为 sum - 1 1 #include <bits/stdc++.h> 2 using namespace std ; 3 4 const

C. Learning Languages 求联通块的个数

C. Learning Languages 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <string> 7 #include <vector> 8 #include <stack> 9 #include <queue&

BZOJ3296: [USACO2011 Open] Learning Languages

3296: [USACO2011 Open] Learning Languages Time Limit: 5 Sec  Memory Limit: 128 MBSubmit: 81  Solved: 39[Submit][Status] Description 农夫约翰的N(2 <= N<=10,000)头奶牛,编号为1.. N,一共会流利地使用M(1<= M <=30,000)种语言,编号从1 .. M.,第i头,会说K_i(1 <= K_i<= M)种语言,即L_

CodeForces 277A Learning Languages 并查集

The "BerCorp" company has got n employees. These employees can use m approved official languages for the formal correspondence. The languages are numbered with integers from 1 to m. For each employee we have the list of languages, which he knows

Codeforces Round #170 (Div. 2)---C. Learning Languages(并查集)

The "BerCorp" company has got n employees. These employees can use m approved official languages for the formal correspondence. The languages are numbered with integers from 1 to m. For each employee we have the list of languages, which he knows

codeforces 277 A Learning Languages 【DFS 】

n个人,每个人会一些语言,两个人只要有会一门相同的语言就可以交流,问为了让这n个人都交流,至少还得学多少门语言 先根据n个人之间他们会的语言,建边 再dfs找出有多少个联通块ans,再加ans-1条边就可以让他们连通 注意特判一下每个人都会0门语言的情况 1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 #include<algorithm> 5 #include<vector&

Translating Skills 英汉语序的对比及翻译

本文记录于培训课程. ----------------------------------------- word order:Refers to the sorting in language units at all levels in the portfolio (1) We are trying our best to win success.陈述句 (2) Who invented the telescope? 特殊疑问句 (3) What a fine day it is ! 感叹句

Codeforces Round #466 (Div. 2) F - Machine Learning

可以观察到,因为我们答案是通过MEX函数得到,那么假设,当前MEX的值是p,那么这时候一共有1+2+3+4...+p-1个与\(c_1\),\(c_2\),\(c_3\)...\(c_p-1\)相同,一共是p*(p-1)/2个数字,那么MEX值一定不超过\(\sqrt n\).故暴力能统计答案. 对于有修改的询问,我们采用待修改莫队,取块的大小为\(n^{\frac23}\). #include<iostream> #include<cstring> #include<alg

SOME USEFUL MACHINE LEARNING LIBRARIES.

from: http://www.erogol.com/broad-view-machine-learning-libraries/ http://www.slideshare.net/VincenzoLomonaco/deep-learning-libraries-and-rst-experiments-with-theano FEBRUARY 6, 2014 EREN 1 COMMENT Especially, with the advent of many different and in

Codeforces Round #315 (Div. 1)

A. Primes or Palindromes? time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard output Rikhail Mubinchik believes that the current definition of prime numbers is obsolete as they are too complex and un