#include <bits/stdc++.h>
#define ll long long
#define pi pair<int, char>
#define pint pair<int, int>
#define ff first
#define ss second
#define pb push_back
#define all(v) v.begin(), v.end()
#define allsir(v) v+1, v+n+1
#define inf 1e9
using namespace std;
const string file = "";
ifstream fin(file+".in");
ofstream fout(file+".out");
const int dim = 250001, mod = 1e9+7;
int n, m, a, b, nod, i, drum[dim];
char c;
vector<pi> g[dim];
void bfs(int nod)
{
int i;
for (i = 1; i <= n; i++)
drum[i] = inf;
drum[nod] = 0;
queue<pint> q;
q.push({0, nod});
while (!q.empty())
{
auto top = q.front();
q.pop();
int dist = top.ff, a = top.ss;
for (auto x : g[a])
{
int b = x.ff, cost = 0;
char tip = x.ss;
if (tip == 'A')
cost = 1;
if (drum[b] > drum[a]+cost)
drum[b] = drum[a]+cost, q.push({drum[b], b});
}
}
}
void solve()
{
cin >> n >> m;
bool allA = 1, allT = 1;
for (i = 1; i <= m; i++)
{
cin >> a >> b >> c;
if (c == 'A')
allT = 0;
else
allA = 0;
g[a].pb({b, c});
g[b].pb({a, c});
}
if (m > 20000)
{
if (allT)
cout << n;
else
{
int rez = 0;
for (i = 1; i <= n; i++)
if (g[i].size() == n-1)
rez++;
cout << rez;
}
return;
}
int rez = 0;
for (nod = 1; nod <= n; nod++)
{
memset(drum, 0, sizeof drum);
bfs(nod);
bool ok = 1;
for (i = 1; i <= n && ok; i++)
if (drum[i] > 1)
ok = 0;
if (ok)
rez++;
}
cout << rez;
}
int main()
{
int t = 1;
ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
//cin >> t;
while (t--)
solve();
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |