K
Khách

Hãy nhập câu hỏi của bạn vào đây, nếu là tài khoản VIP, bạn sẽ được ưu tiên trả lời.

31 tháng 7 2021

#include <bits/stdc++.h>
#define forvct(i,v) for(int i = 0, _key = v.size(); i < _key; ++i)
#define forinc(i,a,b) for(int i = a, _key = b; i <= _key; ++i)
#define fordec(i,a,b) for(int i = a, _key = b; i >= _key; --i)
#define p_b push_back
#define ll long long
#define nn 100001

using namespace std;

int n, m, l, q, t, res, test,
    a[nn], tin[nn], tout[nn], mark[nn], terror[nn], p[nn][20];
vector<int> adj[nn], _adj[nn];
stack<int> stk;

void enter()
{
    cin >> n;
    int u, v;
    forinc(i,1,n-1)
    {
        cin >> u >> v;
        adj[u].p_b(v);
        adj[v].p_b(u);
    }
    l = log2(n);
    cin >> q;
}

void visit(const int &u)
{
    tin[u] = ++t;
    forinc(j,1,l) p[u][j] = p[p[u][j-1]][j-1];
    forvct(j,adj[u])
    {
        int v = adj[u][j];
        if (v != p[u][0])
        {
            p[v][0] = u;
            visit(v);
        }
    }
    tout[u] = ++t;
}

bool anc(const int &u, const int &v)
{
    return tin[u] <= tin[v] && tout[u] >= tout[v];
}

int lca(int u, int v)
{
    if (anc(u,v)) return u;
    if (anc(v,u)) return v;
    fordec(j,l,0)
        if (!anc(p[u][j],v)) u = p[u][j];
    return p[u][0];
}

bool cmp(const int &x, const int &y)
{
    return tin[x] < tin[y];
}

bool check(const int &u)
{
    int cnt = 0;
    forvct(j,_adj[u])
    {
        int v = _adj[u][j];
        if (terror[u] == test)
        {
            if (terror[v] == test && p[v][0] == u)
            {
                res = -1;
                return -1;
            }
            bool x = check(v);
            if (res == -1) return -1;
            res += x;
        }
        else
        {
            bool x = check(v);
            if (res == -1) return -1;
            cnt += x;
        }
    }
    if (terror[u] == test || cnt == 1) return 1;
    if (cnt > 1) res++;
    return 0;
}

void query()
{
    cin >> m;
    forinc(i,1,m)
    {
        cin >> a[i];
        _adj[a[i]].clear();
        mark[a[i]] = test;
        terror[a[i]] = test;
    }
    sort(a+1,a+m+1,cmp);
    forinc(i,1,m-1)
    {
        int tmp = lca(a[i],a[i+1]);
        if (mark[tmp] < test)
        {
            mark[tmp] = test;
            a[++m] = tmp;
            _adj[tmp].clear();
        }
    }
    sort(a+1,a+m+1,cmp);
    while (!stk.empty()) stk.pop();
    stk.push(a[1]);
    forinc(i,2,m)
    {
        while (tout[stk.top()] < tout[a[i]]) stk.pop();
        _adj[stk.top()].p_b(a[i]);
        stk.push(a[i]);
    }
    res = 0;
    check(a[1]);
    cout << res << "\n";
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    enter();
    p[1][0] = 1;
    visit(1);
    for(test = 1; test <= q; ++test) query();
}

 

31 tháng 7 2021

#include <bits/stdc++.h>

using namespace std;

const int maxn = 100010;

vector<int> edge[maxn];
int f[maxn];
bool visited[maxn];

void dfs(int u) {
  visited[u] = true;
  f[u] = 1;
  for (int v : edge[u]) {
    if (!visited[v]) {
      dfs(v);
      f[u] += f[v];
    }
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int u, v;
    cin >> u >> v;
    edge[u].push_back(v);
    edge[v].push_back(u);
  }
  vector<int> components;
  for (int i = 1; i <= n; i++) {
    if (!visited[i]) {
      dfs(i);
      components.push_back(f[i]);
    }
  }
  int res = components.front();
  int rest = 0;
  for (int i = 1; i < components.size(); i++) {
    rest = max(rest, components[i]);
  }
  cout << res + rest << endl;
}

 

26 tháng 4 2021

1b 2c 3a 4a 5d 6d

21 tháng 12 2021

#include <bits/stdc++.h>

using namespace std;

double x,ln,nn;

long long n,i,dem;

int main()

{

cin>>n;

dem=0;

ln=0;

nn=10;

for (i=1; i<=n; i++)

{

cin>>x;

if (x>=5.0) dem++;

ln=max(ln,x);

nn=min(nn,x);

}

cout<<ln<<" "<<nn<<" "<<dem;

return 0;

}

6 tháng 5 2022

lx

6 tháng 5 2022

giúp gì

21 tháng 12 2022

câu lệnh sau vòng for được lặp lại 13-7+1=7 lần