277A - Learning Languages

Click to show code.


using namespace std;
int n, m;
int parent[2 * 110];
int siz[2 * 110];
void make_set(int v)
{
    parent[v] = v;
    siz[v] = 1;
}
int find_set(int v)
{
    if (parent[v] == v)
        return v;
    return find_set(parent[v]);
}
void union_sets(int u, int v)
{
    u = find_set(u);
    v = find_set(v);
    if (u != v)
    {
        parent[v] = u;
        siz[u] += siz[v];
    }
}
int main(void)
{
    int ki, aij, count = 0;
    bool isolated = true;
    cin >> n >> m;
    for (int i = 0, len = n + m; i < len; ++i)
        make_set(i);
    for (int i = 0; i < n; ++i)
    {
        cin >> ki;
        while (ki--)
        {
            cin >> aij;
            union_sets(i, aij - 1 + n);
        }
    }
    for (int i = 0; i < n; ++i)
    {
        if (siz[i] != 1)
            isolated = false;
        if (parent[i] == i)
            ++count;
    }
    cout << count - not(isolated) << endl;
    return 0;
}


276D - Little Girl Maximum Xor

Click to show code.


using namespace std;
using ll = unsigned long long;
using ii = pair<int, int>;
using vi = vector<int>;
inline bool different_bits(ll x, ll y, int i)
{
    return ((x >> i) ^ (y >> i)) & 1LL;
}
ll solve(ll l, ll r)
{
    ll ans = 0, bit_period = 1, diff = r - l;
    int i = 0;
    while (diff >= bit_period)
    {
        ans += bit_period;
        bit_period *= 2;
        i++;
    }
    while (r >= bit_period)
    {
        if (different_bits(l, r, i))
            ans += bit_period;
        bit_period *= 2;
        i++;
    }
    return ans;
}
int main(void)
{
    ll l, r;
    cin >> l >> r;
    cout << solve(l, r) << endl;
    return 0;
}


274B - Zero Tree

Click to show code.


using namespace std;
using ll = long long;
using vi = vector<int>;
int const NMAX = 1e5 + 11;
int n;
ll val[NMAX];
vi g[NMAX];
array<ll, 2> dfs(int u, int p)
{
    array<ll, 2> ans = {0, 0};
    for (auto v : g[u])
    {
        if (v == p)
            continue;
        auto cur = dfs(v, u);
        ans = {max(ans[0], cur[0]), max(ans[1], cur[1])};
    }
    val[u] += ans[0] - ans[1];
    ans[val[u] >= 0] += abs(val[u]);
    val[u] = 0;
    return ans;
}
int main(void)
{
    int u, v;
    cin >> n;
    for (int i = 0; i < n - 1; ++i)
    {
        cin >> u >> v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    for (int i = 1; i <= n; ++i)
        cin >> val[i];
    auto ans = dfs(1, -1);
    cout << accumulate(ans.begin(), ans.end(), 0LL) << endl;
    return 0;
}


271A - Beautiful Year

Click to show code.


using namespace std;
int digit_at(int number, int pos) {
  return number / (1000 / (int)pow(10, pos)) % 10;
}
bool is_valid(int y1, int y2) {
  set<int> digits;
  for (int i = 0; i < 4; ++i) {
    int n = digit_at(y2, i);
    if (digits.find(n) != digits.end())
      return false;
    digits.insert(n);
  }
  return true;
}
int get_next(int year) {
  int y = year;
  while (not is_valid(year, ++y))
    ;
  return y;
}
int main(void) {
  int year;
  cin >> year;
  cout << get_next(year) << endl;
  return 0;
}


268A - Games

Click to show code.


using namespace std;
using vi = vector<int>;
int main(void)
{
    int n, ans = 0;
    cin >> n;
    vi h(n), a(n);
    for (int i = 0; i < n; ++i)
        cin >> h[i] >> a[i];
    for (int i = 0; i < n; ++i)
    {
        for (int j = 0; j < n; ++j)
        {
            if (i == j)
                continue;
            ans += (h[i] == a[j]);
        }
    }
    cout << ans << endl;
    return 0;
}


266A - Stones On Table

Click to show code.


using namespace std;
int main(void)
{
    int n, last, ans = 0;
    string s;
    cin >> n >> s;
    last = 0;
    for (auto c : s)
    {
        if (c == last)
            ++ans;
        last = c;
    }
    cout << ans << endl;
    return 0;
}


263A - Beautiful Matrix

Click to show code.


using namespace std;
int main(void)
{
    int cell, ans = 0;
    for (int i = 1; i <= 5; ++i)
    {
        for (int j = 1; j <= 5; ++j)
        {
            cin >> cell;
            if (cell)
                ans = abs(3 - i) + abs(3 - j);
        }
    }
    cout << ans << endl;
    return 0;
}


252B - Unsorting Array

Click to show code.


using namespace std;
using ii = pair<int, int>;
using vi = vector<int>;
ii const EMPTY = {-1, -1};
ii solve(int n, vi &a)
{
    if (n <= 2)
        return EMPTY;
    if (all_of(a.begin(), a.end(), [&a](int x) { return x == a[0]; }))
        return EMPTY;
    for (int i = 0; i < n - 1; ++i)
    {
        for (int j = i + 1; j < n; ++j)
        {
            if (a[i] != a[j])
            {
                swap(a[i], a[j]);
                if (not is_sorted(a.begin(), a.end()) and
                    not is_sorted(a.begin(), a.end(), greater<int>()))
                    return {i + 1, j + 1};
                swap(a[i], a[j]);
            }
        }
    }
    return EMPTY;
}
int main(void)
{
    ios_base::sync_with_stdio(false), cin.tie(NULL);
    int n;
    vi a;
    cin >> n;
    a.resize(n);
    for (auto &ai : a)
        cin >> ai;
    if (auto ans = solve(n, a); ans != EMPTY)
        cout << ans.first << " " << ans.second << endl;
    else
        cout << "-1" << endl;
    return 0;
}


236A - Boy Or Girl

Click to show code.


using namespace std;
int main(void)
{
    string s;
    vector<bool> alph(26, false);
    string msg[2] = {"CHAT WITH HER!", "IGNORE HIM!"};
    cin >> s;
    for (auto c : s)
        alph[c - 'a'] = true;
    bool ans = accumulate(alph.begin(), alph.end(), 0) % 2;
    cout << msg[ans] << endl;
    return 0;
}


231A - Team

Click to show code.


using namespace std;
int main(void)
{
    int n, cnt, ans = 0;
    bool sure;
    cin >> n;
    for (int i = 0; i < n; ++i)
    {
        cnt = 0;
        for (int j = 0; j < 3; ++j)
        {
            cin >> sure;
            cnt += sure;
        }
        if (cnt >= 2)
            ++ans;
    }
    cout << ans << endl;
    return 0;
}