Blue Bridge 31 days | 3 questions today Day7 | C++

1. Multiply

insert image description here

#include <iostream>
using namespace std;
int main()
{
    
    
  for(long long i=1;i<=1000000007;i++){
    
    
    if((i*2021)%1000000007==999999999){
    
    
            printf("%lld",i);
            break;
    }
  }
  return 0;
}

2. Space

insert image description here

#include <iostream>
using namespace std;
int main()
{
    
    
  //1B=8b
  long long ans=8*256*1024*32;
  printf("%lld",ans);
  return 0;
}

3. Discover the Ring

insert image description here
Idea: The meaning of the question is obviously to find the ring and record the number on the ring, change the writing method of the topological sorting of the directed graph, and save the in-degree in both directions, then the point whose in-degree is read as one enters the queue, and the point that has not been traversed is the ring point on.

The cloud class report is running wrong, the official website is a, I don't know why,
let's study it again

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <queue>
using namespace std;
const int N=1e5+10;
int n;
int h[N],e[N*2],ne[N*2],idx=0;//通过邻接表存储图
int in[N];//记录入度
//h记录表头的边号,e代表表头指向的点,ne表示链表下一条边, idx为边号
bool st[N];//是否走过
vector<bool>ans(N);
void add(int a,int b){
    
    
  e[idx]=b;
  ne[idx]=h[a];
  h[a]=idx++;
  in[b]++;
}
bool dfs(){
    
    
  queue<int>q;
  for(int i=1;i<=n;i++){
    
    
    if(in[i]==1){
    
    
       q.push(i);
    }
  }
  while(!q.empty()){
    
    
    int t=q.front();
    q.pop();
    ans[t]=true;
    for(int i=h[t];i!=-1;i=ne[i]){
    
    
      int x=e[i];
      in[x]--;
      if(in[x]==1){
    
    
        q.push(x);
      }
    }
  }

}
int main()
{
    
    
  int a,b;
  memset(h,-1,sizeof h);
  scanf("%d",&n);
  for(int i=1;i<=n;i++){
    
    
    scanf("%d%d",&a,&b);
    add(a,b);
    add(b,a);
  }
  dfs();
  bool flag=false;
  for(int i=1;i<=n;i++){
    
    
    if(ans[i]==false){
    
    
      if(flag)printf(" ");
      printf("%d",i);
      flag=true;
    }
  }


  return 0;
}


ac code

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#define INF 0x3f3f3f3f
#define ll long long
using namespace std;
const int MAXN = 210000;
struct Edge
{
    
    
    int v,next;
}edge[MAXN];
int head[MAXN],per[MAXN],k,vis[MAXN],len,ans[MAXN];
void Add_edge(int x,int y)
{
    
    
    edge[k].v = y;edge[k].next = head[x];head[x] = k++;
}
bool falg = false;
void dfs(int p,int f)
{
    
    
    for(int i = head[p];i != -1;i = edge[i].next)
    {
    
    
        if(falg) return ;
        int v = edge[i].v;
        if(v == f) continue;
        per[v] = p;
        if(vis[v])
        {
    
    
            ans[len++] = p;
            while(per[p] != v)
            {
    
    
                ans[len++] = per[p];
                p = per[p];
            }
            ans[len++] = v;
            falg = true;
            return ;
        }
        vis[v] = 1;
        dfs(v,p);
    }
    return ;
}
int main()
{
    
    
    int n;
    scanf("%d",&n);
    int x,y;k = 0;len = 0;
    for(int i = 1;i <= n;i ++) per[i] = i;
    memset(head,-1,sizeof(head));
    for(int i = 0;i < n;i ++)
    {
    
    
        scanf("%d%d",&x,&y);
        Add_edge(x,y);
        Add_edge(y,x);
    }
    memset(vis,false,sizeof(vis));
    vis[1] = 1;
    dfs(1,0);
    sort(ans,ans+len);
    for(int i = 0;i < len;i ++)
        printf("%d%c",ans[i],i==len-1?'\n':' ');
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324079532&siteId=291194637