Submission #468452

#TimeUsernameProblemLanguageResultExecution timeMemory
468452myvaluskaMonthly railway pass (LMIO18_menesinis_bilietas)C++14
100 / 100
1008 ms133164 KiB
// trenujsprostasarica.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
// ejoiden2.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
using namespace std;
vector<vector<int>>v;
//vector<int>dx = { -1,1,0,0 };
//vector<int>dy = { 0,0,-1,1 };
vector<int>otec;
vector<int>velkost;
int koren(int vr)
{
    while (otec[vr] != vr)
    {
        vr = otec[vr];
    }
    return vr;
}
void merge(int a, int b)
{
    a = koren(a);
    b = koren(b);
    if (a == b)
    {
        return;
    }
    if (velkost[a] < velkost[b])
    {
        swap(a, b);
    }
    otec[b] = a;
    velkost[a] += velkost[b];
    return;
}
bool suspojene(int a, int b)
{
    a = koren(a);
    b = koren(b);
    if (a == b)
    {
        return true;
    }
    else
    {
        return false;
    }
}
const int maxn = 500036;
set<int>s2[maxn];
int main()
{
    int n;
    cin >> n;
    int m;
    cin >> m;
    v.resize(n);
     velkost.resize(n, 1);
     otec.resize(n);
     for (int i = 0; i < n; i++)
     {
         otec[i] = i;
     }

    for (int i = 0; i < m; i++)
    {
        int a;
        cin >> a;
        int b;
        cin >> b;
        a -= 1;
        b -= 1;
        char typ;
        cin >> typ;
        if (typ == 'T')
        {
            merge(a, b);
        }
        else if (typ == 'A')
        {
            v[a].push_back(b);
            v[b].push_back(a);
        }
    }
    for (int i = 0; i < n; i++)
    {
        otec[i] = koren(i);
    }
    set<int>s;
    for (int i = 0; i < n; i++)
    {
        s.insert(otec[i]);
    }

    for (int i = 0; i < n; i++)
    {
        s2[otec[i]].insert(otec[i]);
        for (int j = 0; j < v[i].size(); j++)
        {
            s2[otec[i]].insert(otec[v[i][j]]);
        }
    }
    int vys = 0;
    for (int i = 0; i < n; i++)
    {
        if (s2[otec[i]].size() >= s.size())
        {
            vys += 1;
        }
    }
    cout << vys << endl;
    return 0;
    //std::cout << "Hello World!\n";
}

// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu

// Tips for Getting Started:
//   1. Use the Solution Explorer window to add/manage files
//   2. Use the Team Explorer window to connect to source control
//   3. Use the Output window to see build output and other messages
//   4. Use the Error List window to view errors
//   5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
//   6. In the future, to open this project again, go to File > Open > Project and select the .sln file

Compilation message (stderr)

menesinis_bilietas.cpp: In function 'int main()':
menesinis_bilietas.cpp:102:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  102 |         for (int j = 0; j < v[i].size(); j++)
      |                         ~~^~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...