Submission #362430

#TimeUsernameProblemLanguageResultExecution timeMemory
362430_martynasMonthly railway pass (LMIO18_menesinis_bilietas)C++11
100 / 100
802 ms87620 KiB
#include <bits/stdc++.h>

using namespace std;

using vi = vector<int>;
const int MAX_N = 5e5+5;
#define PB push_back

vi A[MAX_N];
vi T[MAX_N];
vector<vi> Groups;
int Group[MAX_N];
int group;
bool visited[MAX_N];

int n, m;

void dfs(int curr)
{
    visited[curr] = true;
    Group[curr] = group;
    Groups[group].PB(curr);

    for(int x : T[curr])
    {
        if(!visited[x])
        {
            dfs(x);
        }
    }
}

int main()
{
    //freopen("lmio_2018_3e2_menesinis_bilietas_vyr.in", "r", stdin);
    //freopen("lmio_2018_3e2_menesinis_bilietas_vyr.out", "w", stdout);

    scanf("%d%d", &n, &m);

    for(int i = 0; i < m; i++)
    {
        int a, b;
        char c;
        scanf("%d%d %c", &a, &b, &c);
        a--;
        b--;
        if(c == 'A')
        {
            A[a].PB(b);
            A[b].PB(a);
        }
        else
        {
            T[a].PB(b);
            T[b].PB(a);
        }
    }

    group = 0;

    for(int i = 0; i < n; i++)
    {
        if(!visited[i])
        {
            Groups.PB(vi());
            dfs(i);
            group++;
        }
    }

    int ans = 0;
    set<int> OtherGroups;

    for(vi &g : Groups)
    {
        for(int x : g)
        {
            for(int v : A[x])
            {
                if(Group[v] != Group[x])
                    OtherGroups.insert(Group[v]);
            }
        }
        if(OtherGroups.size() == group - 1)
        {
            ans += g.size();
        }
        OtherGroups.clear();
    }

    printf("%d", ans);

    return 0;
}

Compilation message (stderr)

menesinis_bilietas.cpp: In function 'int main()':
menesinis_bilietas.cpp:84:31: warning: comparison of integer expressions of different signedness: 'std::set<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   84 |         if(OtherGroups.size() == group - 1)
      |            ~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
menesinis_bilietas.cpp:38:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   38 |     scanf("%d%d", &n, &m);
      |     ~~~~~^~~~~~~~~~~~~~~~
menesinis_bilietas.cpp:44:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   44 |         scanf("%d%d %c", &a, &b, &c);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~
#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...