Problem p02737 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

const int maxN = 1e5 + 10, mod = 998244353, lim = 1 << 10;

int n, m;
int ans[maxN + 1], g[maxN + 1], pw[maxN + 1];
int flag[maxN + 1], t[maxN + 1], s[maxN + 1];
bool vis[maxN + 1];

vector<int> nxt[maxN + 1];

inline int ADD(int x, int y) { return x + y >= mod ? x + y - mod : x + y; }

inline int read() {
  int num = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (isdigit(ch))
    num = (num << 3) + (num << 1) + (ch ^ 48), ch = getchar();
  return num * f;
}

inline void dfs(int u) {
  vis[u] = true;
  for (int i = 0; i < nxt[u].size(); i++)
    dfs(nxt[u][i]);
  for (int i = 0; i < nxt[u].size(); i++)
    flag[g[nxt[u][i]]] = u;

  g[u] = 0;
  while (flag[g[u]] == u)
    g[u]++;
}

inline void work() {
  m = read();
  for (int i = 1; i <= n; i++)
    vis[i] = false, nxt[i].clear();
  for (int i = 0; i < lim; i++)
    flag[i] = s[i] = 0;

  for (int i = 1; i <= m; i++) {
    int x = read(), y = read();
    if (x > y)
      swap(x, y);
    nxt[x].push_back(y);
  }

  for (int i = 1; i <= n; i++)
    if (!vis[i])
      dfs(i);

  for (int i = 1; i <= n; i++)
    s[g[i]] = ADD(s[g[i]], pw[i]);

  for (int i = 0; i < lim; i++)
    t[i] = ans[i], ans[i] = 0;
  for (int i = 0; i < lim; i++)
    for (int j = 0; j < lim; j++)
      ans[i ^ j] = ADD(ans[i ^ j], 1ll * t[i] * s[j] % mod);
}

int main() {
  int base = 1;
  for (int i = 1; i <= 18; i++)
    base = 10ll * base % mod;

  n = read();
  pw[0] = 1;
  for (int i = 1; i <= n; i++)
    pw[i] = 1ll * base * pw[i - 1] % mod;

  ans[0] = 1;
  for (int i = 1; i <= 3; i++)
    work();

  printf("%d", ans[0]);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

const int maxN = 1e5 + 10, mod = 998244353, lim = 1 << 10;

int n, m;
int ans[maxN + 1], g[maxN + 1], pw[maxN + 1];
int flag[maxN + 1], t[maxN + 1], s[maxN + 1];
bool vis[maxN + 1];

vector<int> nxt[maxN + 1];

inline int ADD(int x, int y) { return x + y >= mod ? x + y - mod : x + y; }

inline int read() {
  int num = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (isdigit(ch))
    num = (num << 3) + (num << 1) + (ch ^ 48), ch = getchar();
  return num * f;
}

inline void dfs(int u) {
  vis[u] = true;
  for (int i = 0; i < nxt[u].size(); i++)
    if (!vis[nxt[u][i]])
      dfs(nxt[u][i]);
  for (int i = 0; i < nxt[u].size(); i++)
    flag[g[nxt[u][i]]] = u;

  g[u] = 0;
  while (flag[g[u]] == u)
    g[u]++;
}

inline void work() {
  m = read();
  for (int i = 1; i <= n; i++)
    vis[i] = false, nxt[i].clear();
  for (int i = 0; i < lim; i++)
    flag[i] = s[i] = 0;

  for (int i = 1; i <= m; i++) {
    int x = read(), y = read();
    if (x > y)
      swap(x, y);
    nxt[x].push_back(y);
  }

  for (int i = 1; i <= n; i++)
    if (!vis[i])
      dfs(i);

  for (int i = 1; i <= n; i++)
    s[g[i]] = ADD(s[g[i]], pw[i]);

  for (int i = 0; i < lim; i++)
    t[i] = ans[i], ans[i] = 0;
  for (int i = 0; i < lim; i++)
    for (int j = 0; j < lim; j++)
      ans[i ^ j] = ADD(ans[i ^ j], 1ll * t[i] * s[j] % mod);
}

int main() {
  int base = 1;
  for (int i = 1; i <= 18; i++)
    base = 10ll * base % mod;

  n = read();
  pw[0] = 1;
  for (int i = 1; i <= n; i++)
    pw[i] = 1ll * base * pw[i - 1] % mod;

  ans[0] = 1;
  for (int i = 1; i <= 3; i++)
    work();

  printf("%d", ans[0]);
  return 0;
}