Problem p03306 (C++)

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

typedef long long LL;
typedef pair<LL, LL> pii;
#define F first
#define S second
#define MP make_pair

pii operator+(const pii &p1, const pii &p2) {
  return make_pair(p1.F + p2.F, p1.S + p2.S);
}

pii operator-(const pii &p1, const pii &p2) {
  return make_pair(p1.F - p2.F, p1.S - p2.S);
}

pii operator-(const pii &p1) { return make_pair(-p1.F, -p1.S); }

const int N = 100006;

vector<int> G[N];

int u[N], v[N], s[N], e[N];

pii p[N];
bool vis[N];

set<LL> ans;

void check(int i, int j, LL s) {
  if ((p[i] + p[j]) == MP(0LL, s))
    return;
  pii tmp = (p[i] + p[j]);
  s -= tmp.S;
  if (tmp.F == 0) {
    puts("0");
    exit(0);
  } else if (s % (abs(tmp.F)) != 0) {
    puts("0");
    exit(0);
  } else {
    ans.insert(s / tmp.F);
  }
  if (ans.size() >= 2) {
    puts("0");
    exit(0);
  }
}

void dfs(int now) {
  vis[now] = true;
  // cout << "now = " << now << endl;
  for (int ii : G[now]) {
    int i = (now ^ e[ii]);
    if (vis[i]) {
      check(now, i, s[ii]);
    } else {
      p[i] = (MP(0, s[ii]) - p[now]);
      dfs(i);
    }
  }
}

int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  for (int i = 1; m >= i; i++) {
    scanf("%d %d %d", &u[i], &v[i], &s[i]);
    G[u[i]].push_back(i);
    G[v[i]].push_back(i);
    e[i] = (u[i] ^ v[i]);
  }
  p[1] = MP(1, 0); // p_1 = x
  dfs(1);
  if (ans.empty()) {
    // puts("> 1");
    // assume x = 0
    LL INF = (1LL << 61);
    LL pos_mx = -INF, pos_mn = INF;
    LL neg_mx = -INF, neg_mn = INF;
    for (int i = 1; n >= i; i++) {
      // cout << "i = " << i << " , p = " << p[i].F << " , " << p[i].S << endl;
      if (p[i].F == 1) {
        pos_mx = max(pos_mx, p[i].S);
        pos_mn = min(pos_mn, p[i].S);
      } else if (p[i].F == -1) {
        neg_mx = max(neg_mx, p[i].S);
        neg_mn = min(neg_mn, p[i].S);
      } else
        assert(0);
    }
    // cout << "pos_mn = " << pos_mn << " , neg_mx = " << neg_mx << endl;
    LL pos_L = -pos_mn + 1, pos_R = neg_mn - 1;
    printf("%lld\n", max(0LL, pos_R - pos_L + 1));
  } else {
    int _ = (*ans.begin());
    for (int i = 1; n >= i; i++) {
      int val = p[i].first * _ + p[i].second;
      if (val < 0) {
        puts("0");
        return 0;
      }
    }
    puts("1");
  }
}


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

typedef long long LL;
typedef pair<LL, LL> pii;
#define F first
#define S second
#define MP make_pair

pii operator+(const pii &p1, const pii &p2) {
  return make_pair(p1.F + p2.F, p1.S + p2.S);
}

pii operator-(const pii &p1, const pii &p2) {
  return make_pair(p1.F - p2.F, p1.S - p2.S);
}

pii operator-(const pii &p1) { return make_pair(-p1.F, -p1.S); }

const int N = 100006;

vector<int> G[N];

int u[N], v[N], s[N], e[N];

pii p[N];
bool vis[N];

set<LL> ans;

void check(int i, int j, LL s) {
  if ((p[i] + p[j]) == MP(0LL, s))
    return;
  pii tmp = (p[i] + p[j]);
  s -= tmp.S;
  if (tmp.F == 0) {
    puts("0");
    exit(0);
  } else if (s % (abs(tmp.F)) != 0) {
    puts("0");
    exit(0);
  } else {
    ans.insert(s / tmp.F);
  }
  if (ans.size() >= 2) {
    puts("0");
    exit(0);
  }
}

void dfs(int now) {
  vis[now] = true;
  // cout << "now = " << now << endl;
  for (int ii : G[now]) {
    int i = (now ^ e[ii]);
    // cout << "now = " << now << " , i = " << i << " , ii = " << ii << endl;
    if (vis[i]) {
      check(now, i, s[ii]);
    } else {
      p[i] = (MP(0, s[ii]) - p[now]);
      dfs(i);
    }
  }
}

int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  for (int i = 1; m >= i; i++) {
    scanf("%d %d %d", &u[i], &v[i], &s[i]);
    G[u[i]].push_back(i);
    G[v[i]].push_back(i);
    e[i] = (u[i] ^ v[i]);
  }
  p[1] = MP(1, 0); // p_1 = x
  dfs(1);
  if (ans.empty()) {
    // puts("> 1");
    // assume x = 0
    LL INF = (1LL << 61);
    LL pos_mx = -INF, pos_mn = INF;
    LL neg_mx = -INF, neg_mn = INF;
    for (int i = 1; n >= i; i++) {
      // cout << "i = " << i << " , p = " << p[i].F << " , " << p[i].S << endl;
      if (p[i].F == 1) {
        pos_mx = max(pos_mx, p[i].S);
        pos_mn = min(pos_mn, p[i].S);
      } else if (p[i].F == -1) {
        neg_mx = max(neg_mx, p[i].S);
        neg_mn = min(neg_mn, p[i].S);
      } else
        assert(0);
    }
    // cout << "pos_mn = " << pos_mn << " , neg_mx = " << neg_mx << endl;
    LL pos_L = -pos_mn + 1, pos_R = neg_mn - 1;
    printf("%lld\n", max(0LL, pos_R - pos_L + 1));
  } else {
    int _ = (*ans.begin());
    for (int i = 1; n >= i; i++) {
      int val = p[i].first * _ + p[i].second;
      if (val < 0) {
        puts("0");
        return 0;
      }
    }
    puts("1");
  }
}